LCOV - code coverage report
Current view: top level - libavcodec - dolby_e.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 425 0.0 %
Date: 2017-12-14 08:27:08 Functions: 0 24 0.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (C) 2017 foo86
       3             :  *
       4             :  * This file is part of FFmpeg.
       5             :  *
       6             :  * FFmpeg is free software; you can redistribute it and/or
       7             :  * modify it under the terms of the GNU Lesser General Public
       8             :  * License as published by the Free Software Foundation; either
       9             :  * version 2.1 of the License, or (at your option) any later version.
      10             :  *
      11             :  * FFmpeg is distributed in the hope that it will be useful,
      12             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14             :  * Lesser General Public License for more details.
      15             :  *
      16             :  * You should have received a copy of the GNU Lesser General Public
      17             :  * License along with FFmpeg; if not, write to the Free Software
      18             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      19             :  */
      20             : 
      21             : #include "libavutil/float_dsp.h"
      22             : #include "libavutil/thread.h"
      23             : #include "libavutil/mem.h"
      24             : 
      25             : #include "internal.h"
      26             : #include "get_bits.h"
      27             : #include "put_bits.h"
      28             : #include "dolby_e.h"
      29             : #include "fft.h"
      30             : 
      31           0 : static int skip_input(DBEContext *s, int nb_words)
      32             : {
      33           0 :     if (nb_words > s->input_size) {
      34           0 :         av_log(s->avctx, AV_LOG_ERROR, "Packet too short\n");
      35           0 :         return AVERROR_INVALIDDATA;
      36             :     }
      37             : 
      38           0 :     s->input      += nb_words * s->word_bytes;
      39           0 :     s->input_size -= nb_words;
      40           0 :     return 0;
      41             : }
      42             : 
      43           0 : static int parse_key(DBEContext *s)
      44             : {
      45           0 :     if (s->key_present) {
      46           0 :         uint8_t *key = s->input;
      47           0 :         int      ret = skip_input(s, 1);
      48           0 :         if (ret < 0)
      49           0 :             return ret;
      50           0 :         return AV_RB24(key) >> 24 - s->word_bits;
      51             :     }
      52           0 :     return 0;
      53             : }
      54             : 
      55           0 : static int convert_input(DBEContext *s, int nb_words, int key)
      56             : {
      57           0 :     uint8_t *src = s->input;
      58           0 :     uint8_t *dst = s->buffer;
      59             :     PutBitContext pb;
      60             :     int i;
      61             : 
      62           0 :     av_assert0(nb_words <= 1024u);
      63             : 
      64           0 :     if (nb_words > s->input_size) {
      65           0 :         av_log(s->avctx, AV_LOG_ERROR, "Packet too short\n");
      66           0 :         return AVERROR_INVALIDDATA;
      67             :     }
      68             : 
      69           0 :     switch (s->word_bits) {
      70           0 :     case 16:
      71           0 :         for (i = 0; i < nb_words; i++, src += 2, dst += 2)
      72           0 :             AV_WB16(dst, AV_RB16(src) ^ key);
      73           0 :         break;
      74           0 :     case 20:
      75           0 :         init_put_bits(&pb, s->buffer, sizeof(s->buffer));
      76           0 :         for (i = 0; i < nb_words; i++, src += 3)
      77           0 :             put_bits(&pb, 20, AV_RB24(src) >> 4 ^ key);
      78           0 :         flush_put_bits(&pb);
      79           0 :         break;
      80           0 :     case 24:
      81           0 :         for (i = 0; i < nb_words; i++, src += 3, dst += 3)
      82           0 :             AV_WB24(dst, AV_RB24(src) ^ key);
      83           0 :         break;
      84           0 :     default:
      85           0 :         av_assert0(0);
      86             :     }
      87             : 
      88           0 :     return init_get_bits(&s->gb, s->buffer, nb_words * s->word_bits);
      89             : }
      90             : 
      91           0 : static int parse_metadata(DBEContext *s)
      92             : {
      93             :     int i, ret, key, mtd_size;
      94             : 
      95           0 :     if ((key = parse_key(s)) < 0)
      96           0 :         return key;
      97           0 :     if ((ret = convert_input(s, 1, key)) < 0)
      98           0 :         return ret;
      99             : 
     100           0 :     skip_bits(&s->gb, 4);
     101           0 :     mtd_size = get_bits(&s->gb, 10);
     102           0 :     if (!mtd_size) {
     103           0 :         av_log(s->avctx, AV_LOG_ERROR, "Invalid metadata size\n");
     104           0 :         return AVERROR_INVALIDDATA;
     105             :     }
     106             : 
     107           0 :     if ((ret = convert_input(s, mtd_size, key)) < 0)
     108           0 :         return ret;
     109             : 
     110           0 :     skip_bits(&s->gb, 14);
     111           0 :     s->prog_conf = get_bits(&s->gb, 6);
     112           0 :     if (s->prog_conf > MAX_PROG_CONF) {
     113           0 :         av_log(s->avctx, AV_LOG_ERROR, "Invalid program configuration\n");
     114           0 :         return AVERROR_INVALIDDATA;
     115             :     }
     116             : 
     117           0 :     s->nb_channels = nb_channels_tab[s->prog_conf];
     118           0 :     s->nb_programs = nb_programs_tab[s->prog_conf];
     119             : 
     120           0 :     s->fr_code      = get_bits(&s->gb, 4);
     121           0 :     s->fr_code_orig = get_bits(&s->gb, 4);
     122           0 :     if (!sample_rate_tab[s->fr_code] ||
     123           0 :         !sample_rate_tab[s->fr_code_orig]) {
     124           0 :         av_log(s->avctx, AV_LOG_ERROR, "Invalid frame rate code\n");
     125           0 :         return AVERROR_INVALIDDATA;
     126             :     }
     127             : 
     128           0 :     skip_bits_long(&s->gb, 88);
     129           0 :     for (i = 0; i < s->nb_channels; i++)
     130           0 :         s->ch_size[i] = get_bits(&s->gb, 10);
     131           0 :     s->mtd_ext_size = get_bits(&s->gb, 8);
     132           0 :     s->meter_size   = get_bits(&s->gb, 8);
     133             : 
     134           0 :     skip_bits_long(&s->gb, 10 * s->nb_programs);
     135           0 :     for (i = 0; i < s->nb_channels; i++) {
     136           0 :         s->rev_id[i]     = get_bits(&s->gb,  4);
     137           0 :         skip_bits1(&s->gb);
     138           0 :         s->begin_gain[i] = get_bits(&s->gb, 10);
     139           0 :         s->end_gain[i]   = get_bits(&s->gb, 10);
     140             :     }
     141             : 
     142           0 :     if (get_bits_left(&s->gb) < 0) {
     143           0 :         av_log(s->avctx, AV_LOG_ERROR, "Read past end of metadata\n");
     144           0 :         return AVERROR_INVALIDDATA;
     145             :     }
     146             : 
     147           0 :     return skip_input(s, mtd_size + 1);
     148             : }
     149             : 
     150           0 : static int parse_metadata_ext(DBEContext *s)
     151             : {
     152           0 :     if (s->mtd_ext_size)
     153           0 :         return skip_input(s, s->key_present + s->mtd_ext_size + 1);
     154           0 :     return 0;
     155             : }
     156             : 
     157           0 : static void unbias_exponents(DBEContext *s, DBEChannel *c, DBEGroup *g)
     158             : {
     159             :     int mstr_exp[MAX_MSTR_EXP];
     160             :     int bias_exp[MAX_BIAS_EXP];
     161             :     int i, j, k;
     162             : 
     163           0 :     for (i = 0; i < c->nb_mstr_exp; i++)
     164           0 :         mstr_exp[i] = get_bits(&s->gb, 2) * 6;
     165             : 
     166           0 :     for (i = 0; i < g->nb_exponent; i++)
     167           0 :         bias_exp[i] = get_bits(&s->gb, 5);
     168             : 
     169           0 :     for (i = k = 0; i < c->nb_mstr_exp; i++)
     170           0 :         for (j = 0; j < g->nb_bias_exp[i]; j++, k++)
     171           0 :             c->exponents[g->exp_ofs + k] = mstr_exp[i] + bias_exp[k];
     172           0 : }
     173             : 
     174           0 : static int parse_exponents(DBEContext *s, DBEChannel *c)
     175             : {
     176             :     DBEGroup *p, *g;
     177             :     int i;
     178             : 
     179           0 :     for (i = 0, p = NULL, g = c->groups; i < c->nb_groups; i++, p = g, g++) {
     180           0 :         c->exp_strategy[i] = !i || g->nb_exponent != p->nb_exponent || get_bits1(&s->gb);
     181           0 :         if (c->exp_strategy[i]) {
     182           0 :             unbias_exponents(s, c, g);
     183             :         } else {
     184           0 :             memcpy(c->exponents + g->exp_ofs,
     185           0 :                    c->exponents + p->exp_ofs,
     186           0 :                    g->nb_exponent * sizeof(c->exponents[0]));
     187             :         }
     188             :     }
     189             : 
     190           0 :     return 0;
     191             : }
     192             : 
     193           0 : static inline int log_add(int a, int b)
     194             : {
     195           0 :     int c = FFABS(a - b) >> 1;
     196           0 :     return FFMAX(a, b) + log_add_tab[FFMIN(c, 211)];
     197             : }
     198             : 
     199           0 : static void calc_lowcomp(int *msk_val)
     200             : {
     201           0 :     int lwc_val[17] = { 0 };
     202             :     int i, j, k;
     203             : 
     204           0 :     for (i = 0; i < 11; i++) {
     205           0 :         int max_j = 0;
     206           0 :         int max_v = INT_MIN;
     207           0 :         int thr   = 0;
     208             : 
     209           0 :         for (j = FFMAX(i - 3, 0), k = 0; j <= i + 3; j++, k++) {
     210           0 :             int v = msk_val[j] + lwc_gain_tab[i][k];
     211           0 :             if (v > max_v) {
     212           0 :                 max_j = j;
     213           0 :                 max_v = v;
     214             :             }
     215           0 :             thr = log_add(thr, v);
     216             :         }
     217             : 
     218           0 :         if (msk_val[i] < thr) {
     219           0 :             for (j = FFMAX(max_j - 3, 0),
     220           0 :                  k = FFMAX(3 - max_j, 0);
     221           0 :                  j <= max_j + 3; j++, k++)
     222           0 :                 lwc_val[j] += lwc_adj_tab[k];
     223             :         }
     224             :     }
     225             : 
     226           0 :     for (i = 0; i < 16; i++) {
     227           0 :         int v = FFMAX(lwc_val[i], -512);
     228           0 :         msk_val[i] = FFMAX(msk_val[i] + v, 0);
     229             :     }
     230           0 : }
     231             : 
     232           0 : static void bit_allocate(int nb_exponent, int nb_code, int fr_code,
     233             :                          int *exp, int *bap,
     234             :                          int fg_spc, int fg_ofs, int msk_mod, int snr_ofs)
     235             : {
     236             :     int msk_val[MAX_BIAS_EXP];
     237             :     int psd_val[MAX_BIAS_EXP];
     238           0 :     int fast_leak  = 0;
     239           0 :     int slow_leak  = 0;
     240           0 :     int dc_code    = dc_code_tab[fr_code - 1];
     241           0 :     int ht_code    = ht_code_tab[fr_code - 1];
     242           0 :     int fast_gain  = fast_gain_tab[fg_ofs];
     243           0 :     int slow_decay = slow_decay_tab[dc_code][msk_mod];
     244           0 :     int misc_decay = misc_decay_tab[nb_code][dc_code][msk_mod];
     245           0 :     const uint16_t *slow_gain      = slow_gain_tab[nb_code][msk_mod];
     246           0 :     const uint16_t *fast_decay     = fast_decay_tab[nb_code][dc_code][msk_mod];
     247           0 :     const uint16_t *fast_gain_adj  = fast_gain_adj_tab[nb_code][dc_code];
     248           0 :     const uint16_t *hearing_thresh = hearing_thresh_tab[nb_code][ht_code];
     249             :     int i;
     250             : 
     251           0 :     for (i = 0; i < nb_exponent; i++)
     252           0 :         psd_val[i] = (48 - exp[i]) * 64;
     253             : 
     254           0 :     fast_gain_adj += band_ofs_tab[nb_code][fg_spc];
     255           0 :     for (i = 0; i < nb_exponent; i++) {
     256           0 :         fast_leak = log_add(fast_leak  - fast_decay[i],
     257           0 :                             psd_val[i] - fast_gain + fast_gain_adj[i]);
     258           0 :         slow_leak = log_add(slow_leak  - slow_decay,
     259           0 :                             psd_val[i] - slow_gain[i]);
     260           0 :         msk_val[i] = FFMAX(fast_leak, slow_leak);
     261             :     }
     262             : 
     263           0 :     fast_leak = 0;
     264           0 :     for (i = nb_exponent - 1; i > band_low_tab[nb_code]; i--) {
     265           0 :         fast_leak = log_add(fast_leak - misc_decay, psd_val[i] - fast_gain);
     266           0 :         msk_val[i] = FFMAX(msk_val[i], fast_leak);
     267             :     }
     268             : 
     269           0 :     for (i = 0; i < nb_exponent; i++)
     270           0 :         msk_val[i] = FFMAX(msk_val[i], hearing_thresh[i]);
     271             : 
     272           0 :     if (!nb_code)
     273           0 :         calc_lowcomp(msk_val);
     274             : 
     275           0 :     for (i = 0; i < nb_exponent; i++) {
     276           0 :         int v = 16 * (snr_ofs - 64) + psd_val[i] - msk_val[i] >> 5;
     277           0 :         bap[i] = bap_tab[av_clip_uintp2(v, 6)];
     278             :     }
     279           0 : }
     280             : 
     281           0 : static int parse_bit_alloc(DBEContext *s, DBEChannel *c)
     282             : {
     283             :     DBEGroup *p, *g;
     284             :     int bap_strategy[MAX_GROUPS], fg_spc[MAX_GROUPS];
     285             :     int fg_ofs[MAX_GROUPS], msk_mod[MAX_GROUPS];
     286             :     int i, snr_ofs;
     287             : 
     288           0 :     for (i = 0; i < c->nb_groups; i++) {
     289           0 :         bap_strategy[i] = !i || get_bits1(&s->gb);
     290           0 :         if (bap_strategy[i]) {
     291           0 :              fg_spc[i] = get_bits(&s->gb, 2);
     292           0 :              fg_ofs[i] = get_bits(&s->gb, 3);
     293           0 :             msk_mod[i] = get_bits1(&s->gb);
     294             :         } else {
     295           0 :              fg_spc[i] =  fg_spc[i - 1];
     296           0 :              fg_ofs[i] =  fg_ofs[i - 1];
     297           0 :             msk_mod[i] = msk_mod[i - 1];
     298             :         }
     299             :     }
     300             : 
     301           0 :     if (get_bits1(&s->gb)) {
     302           0 :         avpriv_report_missing_feature(s->avctx, "Delta bit allocation");
     303           0 :         return AVERROR_PATCHWELCOME;
     304             :     }
     305             : 
     306           0 :     snr_ofs = get_bits(&s->gb, 8);
     307           0 :     if (!snr_ofs) {
     308           0 :         memset(c->bap, 0, sizeof(c->bap));
     309           0 :         return 0;
     310             :     }
     311             : 
     312           0 :     for (i = 0, p = NULL, g = c->groups; i < c->nb_groups; i++, p = g, g++) {
     313           0 :         if (c->exp_strategy[i] || bap_strategy[i]) {
     314           0 :             bit_allocate(g->nb_exponent, g->imdct_idx, s->fr_code,
     315           0 :                          c->exponents + g->exp_ofs, c->bap + g->exp_ofs,
     316             :                          fg_spc[i], fg_ofs[i], msk_mod[i], snr_ofs);
     317             :         } else {
     318           0 :             memcpy(c->bap + g->exp_ofs,
     319           0 :                    c->bap + p->exp_ofs,
     320           0 :                    g->nb_exponent * sizeof(c->bap[0]));
     321             :         }
     322             :     }
     323             : 
     324           0 :     return 0;
     325             : }
     326             : 
     327           0 : static int parse_indices(DBEContext *s, DBEChannel *c)
     328             : {
     329             :     DBEGroup *p, *g;
     330             :     int i, j;
     331             : 
     332           0 :     for (i = 0, p = NULL, g = c->groups; i < c->nb_groups; i++, p = g, g++) {
     333           0 :         if (get_bits1(&s->gb)) {
     334           0 :             int start = get_bits(&s->gb, 6);
     335             : 
     336           0 :             if (start > g->nb_exponent) {
     337           0 :                 av_log(s->avctx, AV_LOG_ERROR, "Invalid start index\n");
     338           0 :                 return AVERROR_INVALIDDATA;
     339             :             }
     340             : 
     341           0 :             for (j = 0; j < start; j++)
     342           0 :                 c->idx[g->exp_ofs + j] = 0;
     343             : 
     344           0 :             for (; j < g->nb_exponent; j++)
     345           0 :                 c->idx[g->exp_ofs + j] = get_bits(&s->gb, 2);
     346           0 :         } else if (i && g->nb_exponent == p->nb_exponent) {
     347           0 :             memcpy(c->idx + g->exp_ofs,
     348           0 :                    c->idx + p->exp_ofs,
     349           0 :                    g->nb_exponent * sizeof(c->idx[0]));
     350             :         } else {
     351           0 :             memset(c->idx + g->exp_ofs, 0, g->nb_exponent * sizeof(c->idx[0]));
     352             :         }
     353             :     }
     354             : 
     355           0 :     return 0;
     356             : }
     357             : 
     358           0 : static int parse_mantissas(DBEContext *s, DBEChannel *c)
     359             : {
     360             :     DBEGroup *g;
     361             :     int i, j, k;
     362             : 
     363           0 :     for (i = 0, g = c->groups; i < c->nb_groups; i++, g++) {
     364           0 :         float *mnt = c->mantissas + g->mnt_ofs;
     365             : 
     366           0 :         for (j = 0; j < g->nb_exponent; j++) {
     367           0 :             int bap     = c->bap[g->exp_ofs + j];
     368           0 :             int idx     = c->idx[g->exp_ofs + j];
     369           0 :             int size1   = mantissa_size1[bap][idx];
     370           0 :             int count   = g->nb_mantissa[j];
     371           0 :             float exp   = exponent_tab[c->exponents[g->exp_ofs + j]];
     372           0 :             float scale = mantissa_tab1[size1][idx] * exp;
     373             : 
     374           0 :             if (!size1) {
     375           0 :                 memset(mnt, 0, count * sizeof(*mnt));
     376           0 :             } else if (idx) {
     377             :                 int values[100];
     378           0 :                 int escape = -(1 << size1 - 1);
     379             : 
     380           0 :                 for (k = 0; k < count; k++)
     381           0 :                     values[k] = get_sbits(&s->gb, size1);
     382             : 
     383           0 :                 for (k = 0; k < count; k++) {
     384           0 :                     if (values[k] != escape) {
     385           0 :                         mnt[k] = values[k] * scale;
     386             :                     } else {
     387           0 :                         int size2 = mantissa_size2[bap][idx];
     388           0 :                         int value = get_sbits(&s->gb, size2);
     389           0 :                         float a = mantissa_tab2[size2][idx];
     390           0 :                         float b = mantissa_tab3[size2][idx];
     391           0 :                         if (value < 0)
     392           0 :                             mnt[k] = ((value + 1) * a - b) * exp;
     393             :                         else
     394           0 :                             mnt[k] = (value * a + b) * exp;
     395             :                     }
     396             :                 }
     397             :             } else {
     398           0 :                 for (k = 0; k < count; k++)
     399           0 :                     mnt[k] = get_sbits(&s->gb, size1) * scale;
     400             :             }
     401             : 
     402           0 :             mnt += count;
     403             :         }
     404             : 
     405           0 :         for (; j < g->nb_exponent + c->bw_code; j++) {
     406           0 :             memset(mnt, 0, g->nb_mantissa[j] * sizeof(*mnt));
     407           0 :             mnt += g->nb_mantissa[j];
     408             :         }
     409             :     }
     410             : 
     411           0 :     return 0;
     412             : }
     413             : 
     414           0 : static int parse_channel(DBEContext *s, int ch, int seg_id)
     415             : {
     416           0 :     DBEChannel *c = &s->channels[seg_id][ch];
     417             :     int i, ret;
     418             : 
     419           0 :     if (s->rev_id[ch] > 1) {
     420           0 :         avpriv_report_missing_feature(s->avctx, "Encoder revision %d", s->rev_id[ch]);
     421           0 :         return AVERROR_PATCHWELCOME;
     422             :     }
     423             : 
     424           0 :     if (ch == lfe_channel_tab[s->prog_conf]) {
     425           0 :         c->gr_code = 3;
     426           0 :         c->bw_code = 29;
     427             :     } else {
     428           0 :         c->gr_code = get_bits(&s->gb, 2);
     429           0 :         c->bw_code = get_bits(&s->gb, 3);
     430           0 :         if (c->gr_code == 3) {
     431           0 :             av_log(s->avctx, AV_LOG_ERROR, "Invalid group type code\n");
     432           0 :             return AVERROR_INVALIDDATA;
     433             :         }
     434             :     }
     435             : 
     436           0 :     c->nb_groups   = nb_groups_tab[c->gr_code];
     437           0 :     c->nb_mstr_exp = nb_mstr_exp_tab[c->gr_code];
     438             : 
     439           0 :     for (i = 0; i < c->nb_groups; i++) {
     440           0 :         c->groups[i] = frm_ofs_tab[seg_id][c->gr_code][i];
     441           0 :         if (c->nb_mstr_exp == 2) {
     442           0 :             c->groups[i].nb_exponent    -= c->bw_code;
     443           0 :             c->groups[i].nb_bias_exp[1] -= c->bw_code;
     444             :         }
     445             :     }
     446             : 
     447           0 :     if ((ret = parse_exponents(s, c)) < 0)
     448           0 :         return ret;
     449           0 :     if ((ret = parse_bit_alloc(s, c)) < 0)
     450           0 :         return ret;
     451           0 :     if ((ret = parse_indices(s, c)) < 0)
     452           0 :         return ret;
     453           0 :     if ((ret = parse_mantissas(s, c)) < 0)
     454           0 :         return ret;
     455             : 
     456           0 :     if (get_bits_left(&s->gb) < 0) {
     457           0 :         av_log(s->avctx, AV_LOG_ERROR, "Read past end of channel %d\n", ch);
     458           0 :         return AVERROR_INVALIDDATA;
     459             :     }
     460             : 
     461           0 :     return 0;
     462             : }
     463             : 
     464           0 : static int parse_audio(DBEContext *s, int start, int end, int seg_id)
     465             : {
     466             :     int ch, ret, key;
     467             : 
     468           0 :     if ((key = parse_key(s)) < 0)
     469           0 :         return key;
     470             : 
     471           0 :     for (ch = start; ch < end; ch++) {
     472           0 :         if (!s->ch_size[ch]) {
     473           0 :             s->channels[seg_id][ch].nb_groups = 0;
     474           0 :             continue;
     475             :         }
     476           0 :         if ((ret = convert_input(s, s->ch_size[ch], key)) < 0)
     477           0 :             return ret;
     478           0 :         if ((ret = parse_channel(s, ch, seg_id)) < 0) {
     479           0 :             if (s->avctx->err_recognition & AV_EF_EXPLODE)
     480           0 :                 return ret;
     481           0 :             s->channels[seg_id][ch].nb_groups = 0;
     482             :         }
     483           0 :         if ((ret = skip_input(s, s->ch_size[ch])) < 0)
     484           0 :             return ret;
     485             :     }
     486             : 
     487           0 :     return skip_input(s, 1);
     488             : }
     489             : 
     490           0 : static int parse_meter(DBEContext *s)
     491             : {
     492           0 :     if (s->meter_size)
     493           0 :         return skip_input(s, s->key_present + s->meter_size + 1);
     494           0 :     return 0;
     495             : }
     496             : 
     497           0 : static void imdct_calc(DBEContext *s, DBEGroup *g, float *result, float *values)
     498             : {
     499           0 :     FFTContext *imdct = &s->imdct[g->imdct_idx];
     500           0 :     int n   = 1 << imdct_bits_tab[g->imdct_idx];
     501           0 :     int n2  = n >> 1;
     502             :     int i;
     503             : 
     504           0 :     switch (g->imdct_phs) {
     505           0 :     case 0:
     506           0 :         imdct->imdct_half(imdct, result, values);
     507           0 :         for (i = 0; i < n2; i++)
     508           0 :             result[n2 + i] = result[n2 - i - 1];
     509           0 :         break;
     510           0 :     case 1:
     511           0 :         imdct->imdct_calc(imdct, result, values);
     512           0 :         break;
     513           0 :     case 2:
     514           0 :         imdct->imdct_half(imdct, result + n2, values);
     515           0 :         for (i = 0; i < n2; i++)
     516           0 :             result[i] = -result[n - i - 1];
     517           0 :         break;
     518           0 :     default:
     519           0 :         av_assert0(0);
     520             :     }
     521           0 : }
     522             : 
     523           0 : static void transform(DBEContext *s, DBEChannel *c, float *history, float *output)
     524             : {
     525           0 :     LOCAL_ALIGNED_32(float, buffer, [2048]);
     526           0 :     LOCAL_ALIGNED_32(float, result, [1152]);
     527             :     DBEGroup *g;
     528             :     int i;
     529             : 
     530           0 :     memset(result, 0, 1152 * sizeof(float));
     531           0 :     for (i = 0, g = c->groups; i < c->nb_groups; i++, g++) {
     532           0 :         float *src = buffer + g->src_ofs;
     533           0 :         float *dst = result + g->dst_ofs;
     534           0 :         float *win = window + g->win_ofs;
     535             : 
     536           0 :         imdct_calc(s, g, buffer, c->mantissas + g->mnt_ofs);
     537           0 :         s->fdsp->vector_fmul_add(dst, src, win, dst, g->win_len);
     538             :     }
     539             : 
     540           0 :     for (i = 0; i < 256; i++)
     541           0 :         output[i] = history[i] + result[i];
     542           0 :     for (i = 256; i < 896; i++)
     543           0 :         output[i] = result[i];
     544           0 :     for (i = 0; i < 256; i++)
     545           0 :         history[i] = result[896 + i];
     546           0 : }
     547             : 
     548           0 : static void apply_gain(DBEContext *s, int begin, int end, float *output)
     549             : {
     550           0 :     if (begin == 960 && end == 960)
     551           0 :         return;
     552             : 
     553           0 :     if (begin == end) {
     554           0 :         s->fdsp->vector_fmul_scalar(output, output, gain_tab[end], FRAME_SAMPLES);
     555             :     } else {
     556           0 :         float a = gain_tab[begin] * (1.0f / (FRAME_SAMPLES - 1));
     557           0 :         float b = gain_tab[end  ] * (1.0f / (FRAME_SAMPLES - 1));
     558             :         int i;
     559             : 
     560           0 :         for (i = 0; i < FRAME_SAMPLES; i++)
     561           0 :             output[i] *= a * (FRAME_SAMPLES - i - 1) + b * i;
     562             :     }
     563             : }
     564             : 
     565           0 : static int filter_frame(DBEContext *s, AVFrame *frame)
     566             : {
     567             :     const uint8_t *reorder;
     568             :     int ch, ret;
     569             : 
     570           0 :     if (s->nb_channels == 4)
     571           0 :         reorder = ch_reorder_4;
     572           0 :     else if (s->nb_channels == 6)
     573           0 :         reorder = ch_reorder_6;
     574           0 :     else if (s->nb_programs == 1 && !(s->avctx->request_channel_layout & AV_CH_LAYOUT_NATIVE))
     575           0 :         reorder = ch_reorder_8;
     576             :     else
     577           0 :         reorder = ch_reorder_n;
     578             : 
     579           0 :     frame->nb_samples = FRAME_SAMPLES;
     580           0 :     if ((ret = ff_get_buffer(s->avctx, frame, 0)) < 0)
     581           0 :         return ret;
     582             : 
     583           0 :     for (ch = 0; ch < s->nb_channels; ch++) {
     584           0 :         float *output = (float *)frame->extended_data[reorder[ch]];
     585           0 :         transform(s, &s->channels[0][ch], s->history[ch], output);
     586           0 :         transform(s, &s->channels[1][ch], s->history[ch], output + FRAME_SAMPLES / 2);
     587           0 :         apply_gain(s, s->begin_gain[ch], s->end_gain[ch], output);
     588             :     }
     589             : 
     590           0 :     return 0;
     591             : }
     592             : 
     593           0 : static int dolby_e_decode_frame(AVCodecContext *avctx, void *data,
     594             :                                 int *got_frame_ptr, AVPacket *avpkt)
     595             : {
     596           0 :     DBEContext *s = avctx->priv_data;
     597             :     int i, j, hdr, ret;
     598             : 
     599           0 :     if (avpkt->size < 3)
     600           0 :         return AVERROR_INVALIDDATA;
     601             : 
     602           0 :     hdr = AV_RB24(avpkt->data);
     603           0 :     if ((hdr & 0xfffffe) == 0x7888e) {
     604           0 :         s->word_bits = 24;
     605           0 :     } else if ((hdr & 0xffffe0) == 0x788e0) {
     606           0 :         s->word_bits = 20;
     607           0 :     } else if ((hdr & 0xfffe00) == 0x78e00) {
     608           0 :         s->word_bits = 16;
     609             :     } else {
     610           0 :         av_log(avctx, AV_LOG_ERROR, "Invalid frame header\n");
     611           0 :         return AVERROR_INVALIDDATA;
     612             :     }
     613             : 
     614           0 :     s->word_bytes  = s->word_bits + 7 >> 3;
     615           0 :     s->input       = avpkt->data + s->word_bytes;
     616           0 :     s->input_size  = avpkt->size / s->word_bytes - 1;
     617           0 :     s->key_present = hdr >> 24 - s->word_bits & 1;
     618             : 
     619           0 :     if ((ret = parse_metadata(s)) < 0)
     620           0 :         return ret;
     621             : 
     622           0 :     if (s->nb_programs > 1 && !s->multi_prog_warned) {
     623           0 :         av_log(avctx, AV_LOG_WARNING, "Stream has %d programs (configuration %d), "
     624             :                "channels will be output in native order.\n", s->nb_programs, s->prog_conf);
     625           0 :         s->multi_prog_warned = 1;
     626             :     }
     627             : 
     628           0 :     switch (s->nb_channels) {
     629           0 :     case 4:
     630           0 :         avctx->channel_layout = AV_CH_LAYOUT_4POINT0;
     631           0 :         break;
     632           0 :     case 6:
     633           0 :         avctx->channel_layout = AV_CH_LAYOUT_5POINT1;
     634           0 :         break;
     635           0 :     case 8:
     636           0 :         avctx->channel_layout = AV_CH_LAYOUT_7POINT1;
     637           0 :         break;
     638             :     }
     639             : 
     640           0 :     avctx->channels    = s->nb_channels;
     641           0 :     avctx->sample_rate = sample_rate_tab[s->fr_code];
     642           0 :     avctx->sample_fmt  = AV_SAMPLE_FMT_FLTP;
     643             : 
     644           0 :     i = s->nb_channels / 2;
     645           0 :     j = s->nb_channels;
     646           0 :     if ((ret = parse_audio(s, 0, i, 0)) < 0)
     647           0 :         return ret;
     648           0 :     if ((ret = parse_audio(s, i, j, 0)) < 0)
     649           0 :         return ret;
     650           0 :     if ((ret = parse_metadata_ext(s)) < 0)
     651           0 :         return ret;
     652           0 :     if ((ret = parse_audio(s, 0, i, 1)) < 0)
     653           0 :         return ret;
     654           0 :     if ((ret = parse_audio(s, i, j, 1)) < 0)
     655           0 :         return ret;
     656           0 :     if ((ret = parse_meter(s)) < 0)
     657           0 :         return ret;
     658           0 :     if ((ret = filter_frame(s, data)) < 0)
     659           0 :         return ret;
     660             : 
     661           0 :     *got_frame_ptr = 1;
     662           0 :     return avpkt->size;
     663             : }
     664             : 
     665           0 : static av_cold void dolby_e_flush(AVCodecContext *avctx)
     666             : {
     667           0 :     DBEContext *s = avctx->priv_data;
     668             : 
     669           0 :     memset(s->history, 0, sizeof(s->history));
     670           0 : }
     671             : 
     672           0 : static av_cold int dolby_e_close(AVCodecContext *avctx)
     673             : {
     674           0 :     DBEContext *s = avctx->priv_data;
     675             :     int i;
     676             : 
     677           0 :     for (i = 0; i < 3; i++)
     678           0 :         ff_mdct_end(&s->imdct[i]);
     679             : 
     680           0 :     av_freep(&s->fdsp);
     681           0 :     return 0;
     682             : }
     683             : 
     684           0 : static av_cold int dolby_e_init(AVCodecContext *avctx)
     685             : {
     686             :     static AVOnce init_once = AV_ONCE_INIT;
     687           0 :     DBEContext *s = avctx->priv_data;
     688             :     int i;
     689             : 
     690           0 :     if (ff_thread_once(&init_once, init_tables))
     691           0 :         return AVERROR_UNKNOWN;
     692             : 
     693           0 :     for (i = 0; i < 3; i++)
     694           0 :         if (ff_mdct_init(&s->imdct[i], imdct_bits_tab[i], 1, 2.0) < 0)
     695           0 :             return AVERROR(ENOMEM);
     696             : 
     697           0 :     if (!(s->fdsp = avpriv_float_dsp_alloc(0)))
     698           0 :         return AVERROR(ENOMEM);
     699             : 
     700           0 :     s->multi_prog_warned = !!(avctx->request_channel_layout & AV_CH_LAYOUT_NATIVE);
     701           0 :     s->avctx = avctx;
     702           0 :     return 0;
     703             : }
     704             : 
     705             : AVCodec ff_dolby_e_decoder = {
     706             :     .name           = "dolby_e",
     707             :     .long_name      = NULL_IF_CONFIG_SMALL("Dolby E"),
     708             :     .type           = AVMEDIA_TYPE_AUDIO,
     709             :     .id             = AV_CODEC_ID_DOLBY_E,
     710             :     .priv_data_size = sizeof(DBEContext),
     711             :     .init           = dolby_e_init,
     712             :     .decode         = dolby_e_decode_frame,
     713             :     .close          = dolby_e_close,
     714             :     .flush          = dolby_e_flush,
     715             :     .capabilities   = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
     716             :     .sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE },
     717             :     .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
     718             : };

Generated by: LCOV version 1.13