2011-06-07 16:47:09 +00:00
|
|
|
/*
|
|
|
|
* E-AC-3 encoder
|
|
|
|
* Copyright (c) 2011 Justin Ruggles <justin.ruggles@gmail.com>
|
|
|
|
*
|
2013-08-15 21:12:51 +00:00
|
|
|
* This file is part of FFmpeg.
|
2011-06-07 16:47:09 +00:00
|
|
|
*
|
2013-08-15 21:12:51 +00:00
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2011-06-07 16:47:09 +00:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
2013-08-15 21:12:51 +00:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2011-06-07 16:47:09 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2013-08-15 21:12:51 +00:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2011-06-07 16:47:09 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* E-AC-3 encoder
|
|
|
|
*/
|
|
|
|
|
2020-12-03 03:13:28 +00:00
|
|
|
#define AC3ENC_FLOAT 1
|
2013-02-01 09:31:59 +00:00
|
|
|
|
|
|
|
#include "libavutil/attributes.h"
|
2011-06-07 16:47:09 +00:00
|
|
|
#include "ac3enc.h"
|
2022-03-16 17:18:28 +00:00
|
|
|
#include "codec_internal.h"
|
2011-06-07 16:47:09 +00:00
|
|
|
#include "eac3enc.h"
|
2011-07-13 16:20:29 +00:00
|
|
|
#include "eac3_data.h"
|
2011-06-07 16:47:09 +00:00
|
|
|
|
2011-06-10 18:57:19 +00:00
|
|
|
|
2012-06-22 23:32:42 +00:00
|
|
|
static const AVClass eac3enc_class = {
|
|
|
|
.class_name = "E-AC-3 Encoder",
|
|
|
|
.item_name = av_default_item_name,
|
2020-12-03 04:32:18 +00:00
|
|
|
.option = &ff_ac3_enc_options[2], /* First two options are AC-3 only. */
|
2012-06-22 23:32:42 +00:00
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
|
|
|
};
|
2011-06-10 18:57:19 +00:00
|
|
|
|
2011-07-13 16:20:29 +00:00
|
|
|
/**
|
|
|
|
* LUT for finding a matching frame exponent strategy index from a set of
|
|
|
|
* exponent strategies for a single channel across all 6 blocks.
|
|
|
|
*/
|
|
|
|
static int8_t eac3_frame_expstr_index_tab[3][4][4][4][4][4];
|
|
|
|
|
|
|
|
|
2013-02-01 09:31:59 +00:00
|
|
|
av_cold void ff_eac3_exponent_init(void)
|
2011-07-13 16:20:29 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
memset(eac3_frame_expstr_index_tab, -1, sizeof(eac3_frame_expstr_index_tab));
|
|
|
|
for (i = 0; i < 32; i++) {
|
|
|
|
eac3_frame_expstr_index_tab[ff_eac3_frm_expstr[i][0]-1]
|
|
|
|
[ff_eac3_frm_expstr[i][1]]
|
|
|
|
[ff_eac3_frm_expstr[i][2]]
|
|
|
|
[ff_eac3_frm_expstr[i][3]]
|
|
|
|
[ff_eac3_frm_expstr[i][4]]
|
|
|
|
[ff_eac3_frm_expstr[i][5]] = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ff_eac3_get_frame_exp_strategy(AC3EncodeContext *s)
|
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
|
2011-07-14 17:02:45 +00:00
|
|
|
if (s->num_blocks < 6) {
|
|
|
|
s->use_frame_exp_strategy = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-13 16:20:29 +00:00
|
|
|
s->use_frame_exp_strategy = 1;
|
|
|
|
for (ch = !s->cpl_on; ch <= s->fbw_channels; ch++) {
|
|
|
|
int expstr = eac3_frame_expstr_index_tab[s->exp_strategy[ch][0]-1]
|
|
|
|
[s->exp_strategy[ch][1]]
|
|
|
|
[s->exp_strategy[ch][2]]
|
|
|
|
[s->exp_strategy[ch][3]]
|
|
|
|
[s->exp_strategy[ch][4]]
|
|
|
|
[s->exp_strategy[ch][5]];
|
|
|
|
if (expstr < 0) {
|
|
|
|
s->use_frame_exp_strategy = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
s->frame_exp_strategy[ch] = expstr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-06-07 16:47:09 +00:00
|
|
|
void ff_eac3_set_cpl_states(AC3EncodeContext *s)
|
|
|
|
{
|
|
|
|
int ch, blk;
|
|
|
|
int first_cpl_coords[AC3_MAX_CHANNELS];
|
|
|
|
|
|
|
|
/* set first cpl coords */
|
|
|
|
for (ch = 1; ch <= s->fbw_channels; ch++)
|
|
|
|
first_cpl_coords[ch] = 1;
|
2011-07-14 17:02:45 +00:00
|
|
|
for (blk = 0; blk < s->num_blocks; blk++) {
|
2011-06-07 16:47:09 +00:00
|
|
|
AC3Block *block = &s->blocks[blk];
|
|
|
|
for (ch = 1; ch <= s->fbw_channels; ch++) {
|
|
|
|
if (block->channel_in_cpl[ch]) {
|
|
|
|
if (first_cpl_coords[ch]) {
|
2011-08-05 20:28:39 +00:00
|
|
|
block->new_cpl_coords[ch] = 2;
|
2011-06-07 16:47:09 +00:00
|
|
|
first_cpl_coords[ch] = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
first_cpl_coords[ch] = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set first cpl leak */
|
2011-07-14 17:02:45 +00:00
|
|
|
for (blk = 0; blk < s->num_blocks; blk++) {
|
2011-06-07 16:47:09 +00:00
|
|
|
AC3Block *block = &s->blocks[blk];
|
|
|
|
if (block->cpl_in_use) {
|
|
|
|
block->new_cpl_leak = 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ff_eac3_output_frame_header(AC3EncodeContext *s)
|
|
|
|
{
|
|
|
|
int blk, ch;
|
|
|
|
AC3EncOptions *opt = &s->options;
|
|
|
|
|
|
|
|
put_bits(&s->pb, 16, 0x0b77); /* sync word */
|
|
|
|
|
|
|
|
/* BSI header */
|
|
|
|
put_bits(&s->pb, 2, 0); /* stream type = independent */
|
|
|
|
put_bits(&s->pb, 3, 0); /* substream id = 0 */
|
|
|
|
put_bits(&s->pb, 11, (s->frame_size / 2) - 1); /* frame size */
|
|
|
|
if (s->bit_alloc.sr_shift) {
|
|
|
|
put_bits(&s->pb, 2, 0x3); /* fscod2 */
|
|
|
|
put_bits(&s->pb, 2, s->bit_alloc.sr_code); /* sample rate code */
|
|
|
|
} else {
|
|
|
|
put_bits(&s->pb, 2, s->bit_alloc.sr_code); /* sample rate code */
|
2011-07-14 17:02:45 +00:00
|
|
|
put_bits(&s->pb, 2, s->num_blks_code); /* number of blocks */
|
2011-06-07 16:47:09 +00:00
|
|
|
}
|
|
|
|
put_bits(&s->pb, 3, s->channel_mode); /* audio coding mode */
|
|
|
|
put_bits(&s->pb, 1, s->lfe_on); /* LFE channel indicator */
|
|
|
|
put_bits(&s->pb, 5, s->bitstream_id); /* bitstream id (EAC3=16) */
|
|
|
|
put_bits(&s->pb, 5, -opt->dialogue_level); /* dialogue normalization level */
|
|
|
|
put_bits(&s->pb, 1, 0); /* no compression gain */
|
2011-05-29 23:16:46 +00:00
|
|
|
/* mixing metadata*/
|
|
|
|
put_bits(&s->pb, 1, opt->eac3_mixing_metadata);
|
|
|
|
if (opt->eac3_mixing_metadata) {
|
|
|
|
if (s->channel_mode > AC3_CHMODE_STEREO)
|
|
|
|
put_bits(&s->pb, 2, opt->preferred_stereo_downmix);
|
|
|
|
if (s->has_center) {
|
|
|
|
put_bits(&s->pb, 3, s->ltrt_center_mix_level);
|
|
|
|
put_bits(&s->pb, 3, s->loro_center_mix_level);
|
|
|
|
}
|
|
|
|
if (s->has_surround) {
|
|
|
|
put_bits(&s->pb, 3, s->ltrt_surround_mix_level);
|
|
|
|
put_bits(&s->pb, 3, s->loro_surround_mix_level);
|
|
|
|
}
|
|
|
|
if (s->lfe_on)
|
|
|
|
put_bits(&s->pb, 1, 0);
|
|
|
|
put_bits(&s->pb, 1, 0); /* no program scale */
|
|
|
|
put_bits(&s->pb, 1, 0); /* no ext program scale */
|
|
|
|
put_bits(&s->pb, 2, 0); /* no mixing parameters */
|
|
|
|
if (s->channel_mode < AC3_CHMODE_STEREO)
|
|
|
|
put_bits(&s->pb, 1, 0); /* no pan info */
|
|
|
|
put_bits(&s->pb, 1, 0); /* no frame mix config info */
|
|
|
|
}
|
|
|
|
/* info metadata*/
|
|
|
|
put_bits(&s->pb, 1, opt->eac3_info_metadata);
|
|
|
|
if (opt->eac3_info_metadata) {
|
|
|
|
put_bits(&s->pb, 3, s->bitstream_mode);
|
|
|
|
put_bits(&s->pb, 1, opt->copyright);
|
|
|
|
put_bits(&s->pb, 1, opt->original);
|
|
|
|
if (s->channel_mode == AC3_CHMODE_STEREO) {
|
|
|
|
put_bits(&s->pb, 2, opt->dolby_surround_mode);
|
|
|
|
put_bits(&s->pb, 2, opt->dolby_headphone_mode);
|
|
|
|
}
|
|
|
|
if (s->channel_mode >= AC3_CHMODE_2F2R)
|
|
|
|
put_bits(&s->pb, 2, opt->dolby_surround_ex_mode);
|
|
|
|
put_bits(&s->pb, 1, opt->audio_production_info);
|
|
|
|
if (opt->audio_production_info) {
|
|
|
|
put_bits(&s->pb, 5, opt->mixing_level - 80);
|
|
|
|
put_bits(&s->pb, 2, opt->room_type);
|
|
|
|
put_bits(&s->pb, 1, opt->ad_converter_type);
|
|
|
|
}
|
|
|
|
put_bits(&s->pb, 1, 0);
|
|
|
|
}
|
2011-07-14 17:02:45 +00:00
|
|
|
if (s->num_blocks != 6)
|
2023-01-23 23:35:54 +00:00
|
|
|
put_bits(&s->pb, 1, !(s->avctx->frame_num % 6)); /* converter sync flag */
|
2011-06-07 16:47:09 +00:00
|
|
|
put_bits(&s->pb, 1, 0); /* no additional bit stream info */
|
|
|
|
|
|
|
|
/* frame header */
|
2011-07-14 17:02:45 +00:00
|
|
|
if (s->num_blocks == 6) {
|
2021-03-30 18:01:17 +00:00
|
|
|
put_bits(&s->pb, 1, !s->use_frame_exp_strategy); /* exponent strategy syntax */
|
|
|
|
put_bits(&s->pb, 1, 0); /* aht enabled = no */
|
2011-07-14 17:02:45 +00:00
|
|
|
}
|
2011-06-07 16:47:09 +00:00
|
|
|
put_bits(&s->pb, 2, 0); /* snr offset strategy = 1 */
|
|
|
|
put_bits(&s->pb, 1, 0); /* transient pre-noise processing enabled = no */
|
|
|
|
put_bits(&s->pb, 1, 0); /* block switch syntax enabled = no */
|
|
|
|
put_bits(&s->pb, 1, 0); /* dither flag syntax enabled = no */
|
|
|
|
put_bits(&s->pb, 1, 0); /* bit allocation model syntax enabled = no */
|
|
|
|
put_bits(&s->pb, 1, 0); /* fast gain codes enabled = no */
|
|
|
|
put_bits(&s->pb, 1, 0); /* dba syntax enabled = no */
|
|
|
|
put_bits(&s->pb, 1, 0); /* skip field syntax enabled = no */
|
|
|
|
put_bits(&s->pb, 1, 0); /* spx enabled = no */
|
|
|
|
/* coupling strategy use flags */
|
|
|
|
if (s->channel_mode > AC3_CHMODE_MONO) {
|
|
|
|
put_bits(&s->pb, 1, s->blocks[0].cpl_in_use);
|
2011-07-14 17:02:45 +00:00
|
|
|
for (blk = 1; blk < s->num_blocks; blk++) {
|
2011-06-07 16:47:09 +00:00
|
|
|
AC3Block *block = &s->blocks[blk];
|
|
|
|
put_bits(&s->pb, 1, block->new_cpl_strategy);
|
|
|
|
if (block->new_cpl_strategy)
|
|
|
|
put_bits(&s->pb, 1, block->cpl_in_use);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* exponent strategy */
|
2011-07-13 16:20:29 +00:00
|
|
|
if (s->use_frame_exp_strategy) {
|
|
|
|
for (ch = !s->cpl_on; ch <= s->fbw_channels; ch++)
|
|
|
|
put_bits(&s->pb, 5, s->frame_exp_strategy[ch]);
|
|
|
|
} else {
|
2011-07-14 17:02:45 +00:00
|
|
|
for (blk = 0; blk < s->num_blocks; blk++)
|
2011-07-13 16:49:15 +00:00
|
|
|
for (ch = !s->blocks[blk].cpl_in_use; ch <= s->fbw_channels; ch++)
|
|
|
|
put_bits(&s->pb, 2, s->exp_strategy[ch][blk]);
|
2011-07-13 16:20:29 +00:00
|
|
|
}
|
2011-06-07 16:47:09 +00:00
|
|
|
if (s->lfe_on) {
|
2011-07-14 17:02:45 +00:00
|
|
|
for (blk = 0; blk < s->num_blocks; blk++)
|
2011-06-07 16:47:09 +00:00
|
|
|
put_bits(&s->pb, 1, s->exp_strategy[s->lfe_channel][blk]);
|
|
|
|
}
|
2011-07-14 17:02:45 +00:00
|
|
|
/* E-AC-3 to AC-3 converter exponent strategy (not optional when num blocks == 6) */
|
|
|
|
if (s->num_blocks != 6) {
|
|
|
|
put_bits(&s->pb, 1, 0);
|
|
|
|
} else {
|
2021-03-30 18:01:17 +00:00
|
|
|
for (ch = 1; ch <= s->fbw_channels; ch++) {
|
|
|
|
if (s->use_frame_exp_strategy)
|
|
|
|
put_bits(&s->pb, 5, s->frame_exp_strategy[ch]);
|
|
|
|
else
|
|
|
|
put_bits(&s->pb, 5, 0);
|
|
|
|
}
|
2011-07-14 17:02:45 +00:00
|
|
|
}
|
2011-06-07 16:47:09 +00:00
|
|
|
/* snr offsets */
|
|
|
|
put_bits(&s->pb, 6, s->coarse_snr_offset);
|
|
|
|
put_bits(&s->pb, 4, s->fine_snr_offset[1]);
|
|
|
|
/* block start info */
|
2011-07-14 17:02:45 +00:00
|
|
|
if (s->num_blocks > 1)
|
|
|
|
put_bits(&s->pb, 1, 0);
|
2011-06-07 16:47:09 +00:00
|
|
|
}
|
2011-06-10 18:57:19 +00:00
|
|
|
|
|
|
|
|
2022-03-16 20:09:54 +00:00
|
|
|
const FFCodec ff_eac3_encoder = {
|
|
|
|
.p.name = "eac3",
|
2022-08-29 11:38:02 +00:00
|
|
|
CODEC_LONG_NAME("ATSC A/52 E-AC-3"),
|
2022-03-16 20:09:54 +00:00
|
|
|
.p.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.p.id = AV_CODEC_ID_EAC3,
|
2022-11-27 12:37:10 +00:00
|
|
|
.p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
|
2011-06-10 18:57:19 +00:00
|
|
|
.priv_data_size = sizeof(AC3EncodeContext),
|
2014-06-11 12:32:01 +00:00
|
|
|
.init = ff_ac3_float_encode_init,
|
2022-03-30 21:28:24 +00:00
|
|
|
FF_CODEC_ENCODE_CB(ff_ac3_float_encode_frame),
|
2011-06-10 18:57:19 +00:00
|
|
|
.close = ff_ac3_encode_close,
|
2022-03-16 20:09:54 +00:00
|
|
|
.p.sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLTP,
|
2012-04-06 16:19:39 +00:00
|
|
|
AV_SAMPLE_FMT_NONE },
|
2022-03-16 20:09:54 +00:00
|
|
|
.p.priv_class = &eac3enc_class,
|
|
|
|
.p.supported_samplerates = ff_ac3_sample_rate_tab,
|
2022-09-25 23:10:51 +00:00
|
|
|
CODEC_OLD_CHANNEL_LAYOUTS_ARRAY(ff_ac3_channel_layouts)
|
2022-03-16 20:09:54 +00:00
|
|
|
.p.ch_layouts = ff_ac3_ch_layouts,
|
2020-12-03 04:32:18 +00:00
|
|
|
.defaults = ff_ac3_enc_defaults,
|
2022-07-09 22:05:45 +00:00
|
|
|
.caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
|
2011-06-10 18:57:19 +00:00
|
|
|
};
|