• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavcodec/eac3enc.c

Go to the documentation of this file.
00001 /*
00002  * E-AC-3 encoder
00003  * Copyright (c) 2011 Justin Ruggles <justin.ruggles@gmail.com>
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00027 #define CONFIG_AC3ENC_FLOAT 1
00028 #include "ac3enc.h"
00029 #include "eac3enc.h"
00030 #include "eac3_data.h"
00031 
00032 
00033 #define AC3ENC_TYPE AC3ENC_TYPE_EAC3
00034 #include "ac3enc_opts_template.c"
00035 static const AVClass eac3enc_class = { "E-AC-3 Encoder", av_default_item_name,
00036                                        eac3_options, LIBAVUTIL_VERSION_INT };
00037 
00038 
00043 static int8_t eac3_frame_expstr_index_tab[3][4][4][4][4][4];
00044 
00045 
00046 void ff_eac3_exponent_init(void)
00047 {
00048     int i;
00049 
00050     memset(eac3_frame_expstr_index_tab, -1, sizeof(eac3_frame_expstr_index_tab));
00051     for (i = 0; i < 32; i++) {
00052         eac3_frame_expstr_index_tab[ff_eac3_frm_expstr[i][0]-1]
00053                                    [ff_eac3_frm_expstr[i][1]]
00054                                    [ff_eac3_frm_expstr[i][2]]
00055                                    [ff_eac3_frm_expstr[i][3]]
00056                                    [ff_eac3_frm_expstr[i][4]]
00057                                    [ff_eac3_frm_expstr[i][5]] = i;
00058     }
00059 }
00060 
00061 
00062 void ff_eac3_get_frame_exp_strategy(AC3EncodeContext *s)
00063 {
00064     int ch;
00065 
00066     if (s->num_blocks < 6) {
00067         s->use_frame_exp_strategy = 0;
00068         return;
00069     }
00070 
00071     s->use_frame_exp_strategy = 1;
00072     for (ch = !s->cpl_on; ch <= s->fbw_channels; ch++) {
00073         int expstr = eac3_frame_expstr_index_tab[s->exp_strategy[ch][0]-1]
00074                                                 [s->exp_strategy[ch][1]]
00075                                                 [s->exp_strategy[ch][2]]
00076                                                 [s->exp_strategy[ch][3]]
00077                                                 [s->exp_strategy[ch][4]]
00078                                                 [s->exp_strategy[ch][5]];
00079         if (expstr < 0) {
00080             s->use_frame_exp_strategy = 0;
00081             break;
00082         }
00083         s->frame_exp_strategy[ch] = expstr;
00084     }
00085 }
00086 
00087 
00088 
00089 void ff_eac3_set_cpl_states(AC3EncodeContext *s)
00090 {
00091     int ch, blk;
00092     int first_cpl_coords[AC3_MAX_CHANNELS];
00093 
00094     /* set first cpl coords */
00095     for (ch = 1; ch <= s->fbw_channels; ch++)
00096         first_cpl_coords[ch] = 1;
00097     for (blk = 0; blk < s->num_blocks; blk++) {
00098         AC3Block *block = &s->blocks[blk];
00099         for (ch = 1; ch <= s->fbw_channels; ch++) {
00100             if (block->channel_in_cpl[ch]) {
00101                 if (first_cpl_coords[ch]) {
00102                     block->new_cpl_coords[ch] = 2;
00103                     first_cpl_coords[ch]  = 0;
00104                 }
00105             } else {
00106                 first_cpl_coords[ch] = 1;
00107             }
00108         }
00109     }
00110 
00111     /* set first cpl leak */
00112     for (blk = 0; blk < s->num_blocks; blk++) {
00113         AC3Block *block = &s->blocks[blk];
00114         if (block->cpl_in_use) {
00115             block->new_cpl_leak = 2;
00116             break;
00117         }
00118     }
00119 }
00120 
00121 
00122 void ff_eac3_output_frame_header(AC3EncodeContext *s)
00123 {
00124     int blk, ch;
00125     AC3EncOptions *opt = &s->options;
00126 
00127     put_bits(&s->pb, 16, 0x0b77);                   /* sync word */
00128 
00129     /* BSI header */
00130     put_bits(&s->pb,  2, 0);                        /* stream type = independent */
00131     put_bits(&s->pb,  3, 0);                        /* substream id = 0 */
00132     put_bits(&s->pb, 11, (s->frame_size / 2) - 1);  /* frame size */
00133     if (s->bit_alloc.sr_shift) {
00134         put_bits(&s->pb, 2, 0x3);                   /* fscod2 */
00135         put_bits(&s->pb, 2, s->bit_alloc.sr_code);  /* sample rate code */
00136     } else {
00137         put_bits(&s->pb, 2, s->bit_alloc.sr_code);  /* sample rate code */
00138         put_bits(&s->pb, 2, s->num_blks_code);      /* number of blocks */
00139     }
00140     put_bits(&s->pb, 3, s->channel_mode);           /* audio coding mode */
00141     put_bits(&s->pb, 1, s->lfe_on);                 /* LFE channel indicator */
00142     put_bits(&s->pb, 5, s->bitstream_id);           /* bitstream id (EAC3=16) */
00143     put_bits(&s->pb, 5, -opt->dialogue_level);      /* dialogue normalization level */
00144     put_bits(&s->pb, 1, 0);                         /* no compression gain */
00145     /* mixing metadata*/
00146     put_bits(&s->pb, 1, opt->eac3_mixing_metadata);
00147     if (opt->eac3_mixing_metadata) {
00148         if (s->channel_mode > AC3_CHMODE_STEREO)
00149             put_bits(&s->pb, 2, opt->preferred_stereo_downmix);
00150         if (s->has_center) {
00151             put_bits(&s->pb, 3, s->ltrt_center_mix_level);
00152             put_bits(&s->pb, 3, s->loro_center_mix_level);
00153         }
00154         if (s->has_surround) {
00155             put_bits(&s->pb, 3, s->ltrt_surround_mix_level);
00156             put_bits(&s->pb, 3, s->loro_surround_mix_level);
00157         }
00158         if (s->lfe_on)
00159             put_bits(&s->pb, 1, 0);
00160         put_bits(&s->pb, 1, 0);                     /* no program scale */
00161         put_bits(&s->pb, 1, 0);                     /* no ext program scale */
00162         put_bits(&s->pb, 2, 0);                     /* no mixing parameters */
00163         if (s->channel_mode < AC3_CHMODE_STEREO)
00164             put_bits(&s->pb, 1, 0);                 /* no pan info */
00165         put_bits(&s->pb, 1, 0);                     /* no frame mix config info */
00166     }
00167     /* info metadata*/
00168     put_bits(&s->pb, 1, opt->eac3_info_metadata);
00169     if (opt->eac3_info_metadata) {
00170         put_bits(&s->pb, 3, s->bitstream_mode);
00171         put_bits(&s->pb, 1, opt->copyright);
00172         put_bits(&s->pb, 1, opt->original);
00173         if (s->channel_mode == AC3_CHMODE_STEREO) {
00174             put_bits(&s->pb, 2, opt->dolby_surround_mode);
00175             put_bits(&s->pb, 2, opt->dolby_headphone_mode);
00176         }
00177         if (s->channel_mode >= AC3_CHMODE_2F2R)
00178             put_bits(&s->pb, 2, opt->dolby_surround_ex_mode);
00179         put_bits(&s->pb, 1, opt->audio_production_info);
00180         if (opt->audio_production_info) {
00181             put_bits(&s->pb, 5, opt->mixing_level - 80);
00182             put_bits(&s->pb, 2, opt->room_type);
00183             put_bits(&s->pb, 1, opt->ad_converter_type);
00184         }
00185         put_bits(&s->pb, 1, 0);
00186     }
00187     if (s->num_blocks != 6)
00188         put_bits(&s->pb, 1, !(s->avctx->frame_number % 6)); /* converter sync flag */
00189     put_bits(&s->pb, 1, 0);                         /* no additional bit stream info */
00190 
00191     /* frame header */
00192     if (s->num_blocks == 6) {
00193     put_bits(&s->pb, 1, !s->use_frame_exp_strategy);/* exponent strategy syntax */
00194     put_bits(&s->pb, 1, 0);                         /* aht enabled = no */
00195     }
00196     put_bits(&s->pb, 2, 0);                         /* snr offset strategy = 1 */
00197     put_bits(&s->pb, 1, 0);                         /* transient pre-noise processing enabled = no */
00198     put_bits(&s->pb, 1, 0);                         /* block switch syntax enabled = no */
00199     put_bits(&s->pb, 1, 0);                         /* dither flag syntax enabled = no */
00200     put_bits(&s->pb, 1, 0);                         /* bit allocation model syntax enabled = no */
00201     put_bits(&s->pb, 1, 0);                         /* fast gain codes enabled = no */
00202     put_bits(&s->pb, 1, 0);                         /* dba syntax enabled = no */
00203     put_bits(&s->pb, 1, 0);                         /* skip field syntax enabled = no */
00204     put_bits(&s->pb, 1, 0);                         /* spx enabled = no */
00205     /* coupling strategy use flags */
00206     if (s->channel_mode > AC3_CHMODE_MONO) {
00207         put_bits(&s->pb, 1, s->blocks[0].cpl_in_use);
00208         for (blk = 1; blk < s->num_blocks; blk++) {
00209             AC3Block *block = &s->blocks[blk];
00210             put_bits(&s->pb, 1, block->new_cpl_strategy);
00211             if (block->new_cpl_strategy)
00212                 put_bits(&s->pb, 1, block->cpl_in_use);
00213         }
00214     }
00215     /* exponent strategy */
00216     if (s->use_frame_exp_strategy) {
00217         for (ch = !s->cpl_on; ch <= s->fbw_channels; ch++)
00218             put_bits(&s->pb, 5, s->frame_exp_strategy[ch]);
00219     } else {
00220         for (blk = 0; blk < s->num_blocks; blk++)
00221             for (ch = !s->blocks[blk].cpl_in_use; ch <= s->fbw_channels; ch++)
00222                 put_bits(&s->pb, 2, s->exp_strategy[ch][blk]);
00223     }
00224     if (s->lfe_on) {
00225         for (blk = 0; blk < s->num_blocks; blk++)
00226             put_bits(&s->pb, 1, s->exp_strategy[s->lfe_channel][blk]);
00227     }
00228     /* E-AC-3 to AC-3 converter exponent strategy (not optional when num blocks == 6) */
00229     if (s->num_blocks != 6) {
00230         put_bits(&s->pb, 1, 0);
00231     } else {
00232     for (ch = 1; ch <= s->fbw_channels; ch++) {
00233         if (s->use_frame_exp_strategy)
00234             put_bits(&s->pb, 5, s->frame_exp_strategy[ch]);
00235         else
00236             put_bits(&s->pb, 5, 0);
00237     }
00238     }
00239     /* snr offsets */
00240     put_bits(&s->pb, 6, s->coarse_snr_offset);
00241     put_bits(&s->pb, 4, s->fine_snr_offset[1]);
00242     /* block start info */
00243     if (s->num_blocks > 1)
00244         put_bits(&s->pb, 1, 0);
00245 }
00246 
00247 
00248 #if CONFIG_EAC3_ENCODER
00249 AVCodec ff_eac3_encoder = {
00250     .name            = "eac3",
00251     .type            = AVMEDIA_TYPE_AUDIO,
00252     .id              = CODEC_ID_EAC3,
00253     .priv_data_size  = sizeof(AC3EncodeContext),
00254     .init            = ff_ac3_encode_init,
00255     .encode          = ff_ac3_float_encode_frame,
00256     .close           = ff_ac3_encode_close,
00257     .sample_fmts     = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_FLT,AV_SAMPLE_FMT_NONE},
00258     .long_name       = NULL_IF_CONFIG_SMALL("ATSC A/52 E-AC-3"),
00259     .priv_class      = &eac3enc_class,
00260     .channel_layouts = ff_ac3_channel_layouts,
00261 };
00262 #endif
Generated on Sun Apr 22 2012 21:54:00 for Libav by doxygen 1.7.1