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 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
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
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);
00128
00129
00130 put_bits(&s->pb, 2, 0);
00131 put_bits(&s->pb, 3, 0);
00132 put_bits(&s->pb, 11, (s->frame_size / 2) - 1);
00133 if (s->bit_alloc.sr_shift) {
00134 put_bits(&s->pb, 2, 0x3);
00135 put_bits(&s->pb, 2, s->bit_alloc.sr_code);
00136 } else {
00137 put_bits(&s->pb, 2, s->bit_alloc.sr_code);
00138 put_bits(&s->pb, 2, s->num_blks_code);
00139 }
00140 put_bits(&s->pb, 3, s->channel_mode);
00141 put_bits(&s->pb, 1, s->lfe_on);
00142 put_bits(&s->pb, 5, s->bitstream_id);
00143 put_bits(&s->pb, 5, -opt->dialogue_level);
00144 put_bits(&s->pb, 1, 0);
00145
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);
00161 put_bits(&s->pb, 1, 0);
00162 put_bits(&s->pb, 2, 0);
00163 if (s->channel_mode < AC3_CHMODE_STEREO)
00164 put_bits(&s->pb, 1, 0);
00165 put_bits(&s->pb, 1, 0);
00166 }
00167
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));
00189 put_bits(&s->pb, 1, 0);
00190
00191
00192 if (s->num_blocks == 6) {
00193 put_bits(&s->pb, 1, !s->use_frame_exp_strategy);
00194 put_bits(&s->pb, 1, 0);
00195 }
00196 put_bits(&s->pb, 2, 0);
00197 put_bits(&s->pb, 1, 0);
00198 put_bits(&s->pb, 1, 0);
00199 put_bits(&s->pb, 1, 0);
00200 put_bits(&s->pb, 1, 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
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
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
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
00240 put_bits(&s->pb, 6, s->coarse_snr_offset);
00241 put_bits(&s->pb, 4, s->fine_snr_offset[1]);
00242
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 .encode2 = ff_ac3_float_encode_frame,
00256 .close = ff_ac3_encode_close,
00257 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLT,
00258 AV_SAMPLE_FMT_NONE },
00259 .long_name = NULL_IF_CONFIG_SMALL("ATSC A/52 E-AC-3"),
00260 .priv_class = &eac3enc_class,
00261 .channel_layouts = ff_ac3_channel_layouts,
00262 .defaults = ac3_defaults,
00263 };
00264 #endif