00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
00036 static const AVClass eac3enc_class = {
00037 .class_name = "E-AC-3 Encoder",
00038 .item_name = av_default_item_name,
00039 .option = ac3_options,
00040 .version = LIBAVUTIL_VERSION_INT,
00041 };
00042
00047 static int8_t eac3_frame_expstr_index_tab[3][4][4][4][4][4];
00048
00049
00050 void ff_eac3_exponent_init(void)
00051 {
00052 int i;
00053
00054 memset(eac3_frame_expstr_index_tab, -1, sizeof(eac3_frame_expstr_index_tab));
00055 for (i = 0; i < 32; i++) {
00056 eac3_frame_expstr_index_tab[ff_eac3_frm_expstr[i][0]-1]
00057 [ff_eac3_frm_expstr[i][1]]
00058 [ff_eac3_frm_expstr[i][2]]
00059 [ff_eac3_frm_expstr[i][3]]
00060 [ff_eac3_frm_expstr[i][4]]
00061 [ff_eac3_frm_expstr[i][5]] = i;
00062 }
00063 }
00064
00065
00066 void ff_eac3_get_frame_exp_strategy(AC3EncodeContext *s)
00067 {
00068 int ch;
00069
00070 if (s->num_blocks < 6) {
00071 s->use_frame_exp_strategy = 0;
00072 return;
00073 }
00074
00075 s->use_frame_exp_strategy = 1;
00076 for (ch = !s->cpl_on; ch <= s->fbw_channels; ch++) {
00077 int expstr = eac3_frame_expstr_index_tab[s->exp_strategy[ch][0]-1]
00078 [s->exp_strategy[ch][1]]
00079 [s->exp_strategy[ch][2]]
00080 [s->exp_strategy[ch][3]]
00081 [s->exp_strategy[ch][4]]
00082 [s->exp_strategy[ch][5]];
00083 if (expstr < 0) {
00084 s->use_frame_exp_strategy = 0;
00085 break;
00086 }
00087 s->frame_exp_strategy[ch] = expstr;
00088 }
00089 }
00090
00091
00092
00093 void ff_eac3_set_cpl_states(AC3EncodeContext *s)
00094 {
00095 int ch, blk;
00096 int first_cpl_coords[AC3_MAX_CHANNELS];
00097
00098
00099 for (ch = 1; ch <= s->fbw_channels; ch++)
00100 first_cpl_coords[ch] = 1;
00101 for (blk = 0; blk < s->num_blocks; blk++) {
00102 AC3Block *block = &s->blocks[blk];
00103 for (ch = 1; ch <= s->fbw_channels; ch++) {
00104 if (block->channel_in_cpl[ch]) {
00105 if (first_cpl_coords[ch]) {
00106 block->new_cpl_coords[ch] = 2;
00107 first_cpl_coords[ch] = 0;
00108 }
00109 } else {
00110 first_cpl_coords[ch] = 1;
00111 }
00112 }
00113 }
00114
00115
00116 for (blk = 0; blk < s->num_blocks; blk++) {
00117 AC3Block *block = &s->blocks[blk];
00118 if (block->cpl_in_use) {
00119 block->new_cpl_leak = 2;
00120 break;
00121 }
00122 }
00123 }
00124
00125
00126 void ff_eac3_output_frame_header(AC3EncodeContext *s)
00127 {
00128 int blk, ch;
00129 AC3EncOptions *opt = &s->options;
00130
00131 put_bits(&s->pb, 16, 0x0b77);
00132
00133
00134 put_bits(&s->pb, 2, 0);
00135 put_bits(&s->pb, 3, 0);
00136 put_bits(&s->pb, 11, (s->frame_size / 2) - 1);
00137 if (s->bit_alloc.sr_shift) {
00138 put_bits(&s->pb, 2, 0x3);
00139 put_bits(&s->pb, 2, s->bit_alloc.sr_code);
00140 } else {
00141 put_bits(&s->pb, 2, s->bit_alloc.sr_code);
00142 put_bits(&s->pb, 2, s->num_blks_code);
00143 }
00144 put_bits(&s->pb, 3, s->channel_mode);
00145 put_bits(&s->pb, 1, s->lfe_on);
00146 put_bits(&s->pb, 5, s->bitstream_id);
00147 put_bits(&s->pb, 5, -opt->dialogue_level);
00148 put_bits(&s->pb, 1, 0);
00149
00150 put_bits(&s->pb, 1, opt->eac3_mixing_metadata);
00151 if (opt->eac3_mixing_metadata) {
00152 if (s->channel_mode > AC3_CHMODE_STEREO)
00153 put_bits(&s->pb, 2, opt->preferred_stereo_downmix);
00154 if (s->has_center) {
00155 put_bits(&s->pb, 3, s->ltrt_center_mix_level);
00156 put_bits(&s->pb, 3, s->loro_center_mix_level);
00157 }
00158 if (s->has_surround) {
00159 put_bits(&s->pb, 3, s->ltrt_surround_mix_level);
00160 put_bits(&s->pb, 3, s->loro_surround_mix_level);
00161 }
00162 if (s->lfe_on)
00163 put_bits(&s->pb, 1, 0);
00164 put_bits(&s->pb, 1, 0);
00165 put_bits(&s->pb, 1, 0);
00166 put_bits(&s->pb, 2, 0);
00167 if (s->channel_mode < AC3_CHMODE_STEREO)
00168 put_bits(&s->pb, 1, 0);
00169 put_bits(&s->pb, 1, 0);
00170 }
00171
00172 put_bits(&s->pb, 1, opt->eac3_info_metadata);
00173 if (opt->eac3_info_metadata) {
00174 put_bits(&s->pb, 3, s->bitstream_mode);
00175 put_bits(&s->pb, 1, opt->copyright);
00176 put_bits(&s->pb, 1, opt->original);
00177 if (s->channel_mode == AC3_CHMODE_STEREO) {
00178 put_bits(&s->pb, 2, opt->dolby_surround_mode);
00179 put_bits(&s->pb, 2, opt->dolby_headphone_mode);
00180 }
00181 if (s->channel_mode >= AC3_CHMODE_2F2R)
00182 put_bits(&s->pb, 2, opt->dolby_surround_ex_mode);
00183 put_bits(&s->pb, 1, opt->audio_production_info);
00184 if (opt->audio_production_info) {
00185 put_bits(&s->pb, 5, opt->mixing_level - 80);
00186 put_bits(&s->pb, 2, opt->room_type);
00187 put_bits(&s->pb, 1, opt->ad_converter_type);
00188 }
00189 put_bits(&s->pb, 1, 0);
00190 }
00191 if (s->num_blocks != 6)
00192 put_bits(&s->pb, 1, !(s->avctx->frame_number % 6));
00193 put_bits(&s->pb, 1, 0);
00194
00195
00196 if (s->num_blocks == 6) {
00197 put_bits(&s->pb, 1, !s->use_frame_exp_strategy);
00198 put_bits(&s->pb, 1, 0);
00199 }
00200 put_bits(&s->pb, 2, 0);
00201 put_bits(&s->pb, 1, 0);
00202 put_bits(&s->pb, 1, 0);
00203 put_bits(&s->pb, 1, 0);
00204 put_bits(&s->pb, 1, 0);
00205 put_bits(&s->pb, 1, 0);
00206 put_bits(&s->pb, 1, 0);
00207 put_bits(&s->pb, 1, 0);
00208 put_bits(&s->pb, 1, 0);
00209
00210 if (s->channel_mode > AC3_CHMODE_MONO) {
00211 put_bits(&s->pb, 1, s->blocks[0].cpl_in_use);
00212 for (blk = 1; blk < s->num_blocks; blk++) {
00213 AC3Block *block = &s->blocks[blk];
00214 put_bits(&s->pb, 1, block->new_cpl_strategy);
00215 if (block->new_cpl_strategy)
00216 put_bits(&s->pb, 1, block->cpl_in_use);
00217 }
00218 }
00219
00220 if (s->use_frame_exp_strategy) {
00221 for (ch = !s->cpl_on; ch <= s->fbw_channels; ch++)
00222 put_bits(&s->pb, 5, s->frame_exp_strategy[ch]);
00223 } else {
00224 for (blk = 0; blk < s->num_blocks; blk++)
00225 for (ch = !s->blocks[blk].cpl_in_use; ch <= s->fbw_channels; ch++)
00226 put_bits(&s->pb, 2, s->exp_strategy[ch][blk]);
00227 }
00228 if (s->lfe_on) {
00229 for (blk = 0; blk < s->num_blocks; blk++)
00230 put_bits(&s->pb, 1, s->exp_strategy[s->lfe_channel][blk]);
00231 }
00232
00233 if (s->num_blocks != 6) {
00234 put_bits(&s->pb, 1, 0);
00235 } else {
00236 for (ch = 1; ch <= s->fbw_channels; ch++) {
00237 if (s->use_frame_exp_strategy)
00238 put_bits(&s->pb, 5, s->frame_exp_strategy[ch]);
00239 else
00240 put_bits(&s->pb, 5, 0);
00241 }
00242 }
00243
00244 put_bits(&s->pb, 6, s->coarse_snr_offset);
00245 put_bits(&s->pb, 4, s->fine_snr_offset[1]);
00246
00247 if (s->num_blocks > 1)
00248 put_bits(&s->pb, 1, 0);
00249 }
00250
00251
00252 #if CONFIG_EAC3_ENCODER
00253 AVCodec ff_eac3_encoder = {
00254 .name = "eac3",
00255 .type = AVMEDIA_TYPE_AUDIO,
00256 .id = AV_CODEC_ID_EAC3,
00257 .priv_data_size = sizeof(AC3EncodeContext),
00258 .init = ff_ac3_encode_init,
00259 .encode2 = ff_ac3_float_encode_frame,
00260 .close = ff_ac3_encode_close,
00261 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLT,
00262 AV_SAMPLE_FMT_NONE },
00263 .long_name = NULL_IF_CONFIG_SMALL("ATSC A/52 E-AC-3"),
00264 .priv_class = &eac3enc_class,
00265 .channel_layouts = ff_ac3_channel_layouts,
00266 .defaults = ac3_defaults,
00267 };
00268 #endif