LCOV - code coverage report
Current view: top level - libavcodec - eac3enc.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 89 135 65.9 %
Date: 2017-12-12 03:56:30 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /*
       2             :  * E-AC-3 encoder
       3             :  * Copyright (c) 2011 Justin Ruggles <justin.ruggles@gmail.com>
       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             : /**
      23             :  * @file
      24             :  * E-AC-3 encoder
      25             :  */
      26             : 
      27             : #define CONFIG_AC3ENC_FLOAT 1
      28             : 
      29             : #include "libavutil/attributes.h"
      30             : #include "ac3enc.h"
      31             : #include "eac3enc.h"
      32             : #include "eac3_data.h"
      33             : 
      34             : 
      35             : #define AC3ENC_TYPE AC3ENC_TYPE_EAC3
      36             : #include "ac3enc_opts_template.c"
      37             : 
      38             : static const AVClass eac3enc_class = {
      39             :     .class_name = "E-AC-3 Encoder",
      40             :     .item_name  = av_default_item_name,
      41             :     .option     = ac3_options,
      42             :     .version    = LIBAVUTIL_VERSION_INT,
      43             : };
      44             : 
      45             : /**
      46             :  * LUT for finding a matching frame exponent strategy index from a set of
      47             :  * exponent strategies for a single channel across all 6 blocks.
      48             :  */
      49             : static int8_t eac3_frame_expstr_index_tab[3][4][4][4][4][4];
      50             : 
      51             : 
      52           1 : av_cold void ff_eac3_exponent_init(void)
      53             : {
      54             :     int i;
      55             : 
      56           1 :     memset(eac3_frame_expstr_index_tab, -1, sizeof(eac3_frame_expstr_index_tab));
      57          33 :     for (i = 0; i < 32; i++) {
      58          32 :         eac3_frame_expstr_index_tab[ff_eac3_frm_expstr[i][0]-1]
      59          32 :                                    [ff_eac3_frm_expstr[i][1]]
      60          32 :                                    [ff_eac3_frm_expstr[i][2]]
      61          32 :                                    [ff_eac3_frm_expstr[i][3]]
      62          32 :                                    [ff_eac3_frm_expstr[i][4]]
      63          32 :                                    [ff_eac3_frm_expstr[i][5]] = i;
      64             :     }
      65           1 : }
      66             : 
      67             : 
      68         273 : void ff_eac3_get_frame_exp_strategy(AC3EncodeContext *s)
      69             : {
      70             :     int ch;
      71             : 
      72         273 :     if (s->num_blocks < 6) {
      73           0 :         s->use_frame_exp_strategy = 0;
      74           0 :         return;
      75             :     }
      76             : 
      77         273 :     s->use_frame_exp_strategy = 1;
      78        1092 :     for (ch = !s->cpl_on; ch <= s->fbw_channels; ch++) {
      79        1638 :         int expstr = eac3_frame_expstr_index_tab[s->exp_strategy[ch][0]-1]
      80         819 :                                                 [s->exp_strategy[ch][1]]
      81         819 :                                                 [s->exp_strategy[ch][2]]
      82         819 :                                                 [s->exp_strategy[ch][3]]
      83         819 :                                                 [s->exp_strategy[ch][4]]
      84         819 :                                                 [s->exp_strategy[ch][5]];
      85         819 :         if (expstr < 0) {
      86           0 :             s->use_frame_exp_strategy = 0;
      87           0 :             break;
      88             :         }
      89         819 :         s->frame_exp_strategy[ch] = expstr;
      90             :     }
      91             : }
      92             : 
      93             : 
      94             : 
      95         273 : void ff_eac3_set_cpl_states(AC3EncodeContext *s)
      96             : {
      97             :     int ch, blk;
      98             :     int first_cpl_coords[AC3_MAX_CHANNELS];
      99             : 
     100             :     /* set first cpl coords */
     101         819 :     for (ch = 1; ch <= s->fbw_channels; ch++)
     102         546 :         first_cpl_coords[ch] = 1;
     103        1911 :     for (blk = 0; blk < s->num_blocks; blk++) {
     104        1638 :         AC3Block *block = &s->blocks[blk];
     105        4914 :         for (ch = 1; ch <= s->fbw_channels; ch++) {
     106        3276 :             if (block->channel_in_cpl[ch]) {
     107        3276 :                 if (first_cpl_coords[ch]) {
     108         546 :                     block->new_cpl_coords[ch] = 2;
     109         546 :                     first_cpl_coords[ch]  = 0;
     110             :                 }
     111             :             } else {
     112           0 :                 first_cpl_coords[ch] = 1;
     113             :             }
     114             :         }
     115             :     }
     116             : 
     117             :     /* set first cpl leak */
     118         273 :     for (blk = 0; blk < s->num_blocks; blk++) {
     119         273 :         AC3Block *block = &s->blocks[blk];
     120         273 :         if (block->cpl_in_use) {
     121         273 :             block->new_cpl_leak = 2;
     122         273 :             break;
     123             :         }
     124             :     }
     125         273 : }
     126             : 
     127             : 
     128         273 : void ff_eac3_output_frame_header(AC3EncodeContext *s)
     129             : {
     130             :     int blk, ch;
     131         273 :     AC3EncOptions *opt = &s->options;
     132             : 
     133         273 :     put_bits(&s->pb, 16, 0x0b77);                   /* sync word */
     134             : 
     135             :     /* BSI header */
     136         273 :     put_bits(&s->pb,  2, 0);                        /* stream type = independent */
     137         273 :     put_bits(&s->pb,  3, 0);                        /* substream id = 0 */
     138         273 :     put_bits(&s->pb, 11, (s->frame_size / 2) - 1);  /* frame size */
     139         273 :     if (s->bit_alloc.sr_shift) {
     140           0 :         put_bits(&s->pb, 2, 0x3);                   /* fscod2 */
     141           0 :         put_bits(&s->pb, 2, s->bit_alloc.sr_code);  /* sample rate code */
     142             :     } else {
     143         273 :         put_bits(&s->pb, 2, s->bit_alloc.sr_code);  /* sample rate code */
     144         273 :         put_bits(&s->pb, 2, s->num_blks_code);      /* number of blocks */
     145             :     }
     146         273 :     put_bits(&s->pb, 3, s->channel_mode);           /* audio coding mode */
     147         273 :     put_bits(&s->pb, 1, s->lfe_on);                 /* LFE channel indicator */
     148         273 :     put_bits(&s->pb, 5, s->bitstream_id);           /* bitstream id (EAC3=16) */
     149         273 :     put_bits(&s->pb, 5, -opt->dialogue_level);      /* dialogue normalization level */
     150         273 :     put_bits(&s->pb, 1, 0);                         /* no compression gain */
     151             :     /* mixing metadata*/
     152         273 :     put_bits(&s->pb, 1, opt->eac3_mixing_metadata);
     153         273 :     if (opt->eac3_mixing_metadata) {
     154           0 :         if (s->channel_mode > AC3_CHMODE_STEREO)
     155           0 :             put_bits(&s->pb, 2, opt->preferred_stereo_downmix);
     156           0 :         if (s->has_center) {
     157           0 :             put_bits(&s->pb, 3, s->ltrt_center_mix_level);
     158           0 :             put_bits(&s->pb, 3, s->loro_center_mix_level);
     159             :         }
     160           0 :         if (s->has_surround) {
     161           0 :             put_bits(&s->pb, 3, s->ltrt_surround_mix_level);
     162           0 :             put_bits(&s->pb, 3, s->loro_surround_mix_level);
     163             :         }
     164           0 :         if (s->lfe_on)
     165           0 :             put_bits(&s->pb, 1, 0);
     166           0 :         put_bits(&s->pb, 1, 0);                     /* no program scale */
     167           0 :         put_bits(&s->pb, 1, 0);                     /* no ext program scale */
     168           0 :         put_bits(&s->pb, 2, 0);                     /* no mixing parameters */
     169           0 :         if (s->channel_mode < AC3_CHMODE_STEREO)
     170           0 :             put_bits(&s->pb, 1, 0);                 /* no pan info */
     171           0 :         put_bits(&s->pb, 1, 0);                     /* no frame mix config info */
     172             :     }
     173             :     /* info metadata*/
     174         273 :     put_bits(&s->pb, 1, opt->eac3_info_metadata);
     175         273 :     if (opt->eac3_info_metadata) {
     176           0 :         put_bits(&s->pb, 3, s->bitstream_mode);
     177           0 :         put_bits(&s->pb, 1, opt->copyright);
     178           0 :         put_bits(&s->pb, 1, opt->original);
     179           0 :         if (s->channel_mode == AC3_CHMODE_STEREO) {
     180           0 :             put_bits(&s->pb, 2, opt->dolby_surround_mode);
     181           0 :             put_bits(&s->pb, 2, opt->dolby_headphone_mode);
     182             :         }
     183           0 :         if (s->channel_mode >= AC3_CHMODE_2F2R)
     184           0 :             put_bits(&s->pb, 2, opt->dolby_surround_ex_mode);
     185           0 :         put_bits(&s->pb, 1, opt->audio_production_info);
     186           0 :         if (opt->audio_production_info) {
     187           0 :             put_bits(&s->pb, 5, opt->mixing_level - 80);
     188           0 :             put_bits(&s->pb, 2, opt->room_type);
     189           0 :             put_bits(&s->pb, 1, opt->ad_converter_type);
     190             :         }
     191           0 :         put_bits(&s->pb, 1, 0);
     192             :     }
     193         273 :     if (s->num_blocks != 6)
     194           0 :         put_bits(&s->pb, 1, !(s->avctx->frame_number % 6)); /* converter sync flag */
     195         273 :     put_bits(&s->pb, 1, 0);                         /* no additional bit stream info */
     196             : 
     197             :     /* frame header */
     198         273 :     if (s->num_blocks == 6) {
     199         273 :     put_bits(&s->pb, 1, !s->use_frame_exp_strategy);/* exponent strategy syntax */
     200         273 :     put_bits(&s->pb, 1, 0);                         /* aht enabled = no */
     201             :     }
     202         273 :     put_bits(&s->pb, 2, 0);                         /* snr offset strategy = 1 */
     203         273 :     put_bits(&s->pb, 1, 0);                         /* transient pre-noise processing enabled = no */
     204         273 :     put_bits(&s->pb, 1, 0);                         /* block switch syntax enabled = no */
     205         273 :     put_bits(&s->pb, 1, 0);                         /* dither flag syntax enabled = no */
     206         273 :     put_bits(&s->pb, 1, 0);                         /* bit allocation model syntax enabled = no */
     207         273 :     put_bits(&s->pb, 1, 0);                         /* fast gain codes enabled = no */
     208         273 :     put_bits(&s->pb, 1, 0);                         /* dba syntax enabled = no */
     209         273 :     put_bits(&s->pb, 1, 0);                         /* skip field syntax enabled = no */
     210         273 :     put_bits(&s->pb, 1, 0);                         /* spx enabled = no */
     211             :     /* coupling strategy use flags */
     212         273 :     if (s->channel_mode > AC3_CHMODE_MONO) {
     213         273 :         put_bits(&s->pb, 1, s->blocks[0].cpl_in_use);
     214        1638 :         for (blk = 1; blk < s->num_blocks; blk++) {
     215        1365 :             AC3Block *block = &s->blocks[blk];
     216        1365 :             put_bits(&s->pb, 1, block->new_cpl_strategy);
     217        1365 :             if (block->new_cpl_strategy)
     218           0 :                 put_bits(&s->pb, 1, block->cpl_in_use);
     219             :         }
     220             :     }
     221             :     /* exponent strategy */
     222         273 :     if (s->use_frame_exp_strategy) {
     223        1092 :         for (ch = !s->cpl_on; ch <= s->fbw_channels; ch++)
     224         819 :             put_bits(&s->pb, 5, s->frame_exp_strategy[ch]);
     225             :     } else {
     226           0 :         for (blk = 0; blk < s->num_blocks; blk++)
     227           0 :             for (ch = !s->blocks[blk].cpl_in_use; ch <= s->fbw_channels; ch++)
     228           0 :                 put_bits(&s->pb, 2, s->exp_strategy[ch][blk]);
     229             :     }
     230         273 :     if (s->lfe_on) {
     231           0 :         for (blk = 0; blk < s->num_blocks; blk++)
     232           0 :             put_bits(&s->pb, 1, s->exp_strategy[s->lfe_channel][blk]);
     233             :     }
     234             :     /* E-AC-3 to AC-3 converter exponent strategy (not optional when num blocks == 6) */
     235         273 :     if (s->num_blocks != 6) {
     236           0 :         put_bits(&s->pb, 1, 0);
     237             :     } else {
     238         819 :     for (ch = 1; ch <= s->fbw_channels; ch++) {
     239         546 :         if (s->use_frame_exp_strategy)
     240         546 :             put_bits(&s->pb, 5, s->frame_exp_strategy[ch]);
     241             :         else
     242           0 :             put_bits(&s->pb, 5, 0);
     243             :     }
     244             :     }
     245             :     /* snr offsets */
     246         273 :     put_bits(&s->pb, 6, s->coarse_snr_offset);
     247         273 :     put_bits(&s->pb, 4, s->fine_snr_offset[1]);
     248             :     /* block start info */
     249         273 :     if (s->num_blocks > 1)
     250         273 :         put_bits(&s->pb, 1, 0);
     251         273 : }
     252             : 
     253             : 
     254             : AVCodec ff_eac3_encoder = {
     255             :     .name            = "eac3",
     256             :     .long_name       = NULL_IF_CONFIG_SMALL("ATSC A/52 E-AC-3"),
     257             :     .type            = AVMEDIA_TYPE_AUDIO,
     258             :     .id              = AV_CODEC_ID_EAC3,
     259             :     .priv_data_size  = sizeof(AC3EncodeContext),
     260             :     .init            = ff_ac3_float_encode_init,
     261             :     .encode2         = ff_ac3_float_encode_frame,
     262             :     .close           = ff_ac3_encode_close,
     263             :     .sample_fmts     = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLTP,
     264             :                                                       AV_SAMPLE_FMT_NONE },
     265             :     .priv_class      = &eac3enc_class,
     266             :     .channel_layouts = ff_ac3_channel_layouts,
     267             :     .defaults        = ac3_defaults,
     268             : };

Generated by: LCOV version 1.13