eac3enc.c
Go to the documentation of this file.
1 /*
2  * E-AC-3 encoder
3  * Copyright (c) 2011 Justin Ruggles <justin.ruggles@gmail.com>
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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 #include "ac3enc.h"
29 #include "eac3enc.h"
30 #include "eac3_data.h"
31 
32 
33 #define AC3ENC_TYPE AC3ENC_TYPE_EAC3
34 #include "ac3enc_opts_template.c"
35 
36 static const AVClass eac3enc_class = {
37  .class_name = "E-AC-3 Encoder",
38  .item_name = av_default_item_name,
39  .option = ac3_options,
40  .version = LIBAVUTIL_VERSION_INT,
41 };
42 
43 /**
44  * LUT for finding a matching frame exponent strategy index from a set of
45  * exponent strategies for a single channel across all 6 blocks.
46  */
47 static int8_t eac3_frame_expstr_index_tab[3][4][4][4][4][4];
48 
49 
51 {
52  int i;
53 
55  for (i = 0; i < 32; i++) {
57  [ff_eac3_frm_expstr[i][1]]
58  [ff_eac3_frm_expstr[i][2]]
59  [ff_eac3_frm_expstr[i][3]]
60  [ff_eac3_frm_expstr[i][4]]
61  [ff_eac3_frm_expstr[i][5]] = i;
62  }
63 }
64 
65 
67 {
68  int ch;
69 
70  if (s->num_blocks < 6) {
72  return;
73  }
74 
76  for (ch = !s->cpl_on; ch <= s->fbw_channels; ch++) {
77  int expstr = eac3_frame_expstr_index_tab[s->exp_strategy[ch][0]-1]
78  [s->exp_strategy[ch][1]]
79  [s->exp_strategy[ch][2]]
80  [s->exp_strategy[ch][3]]
81  [s->exp_strategy[ch][4]]
82  [s->exp_strategy[ch][5]];
83  if (expstr < 0) {
85  break;
86  }
87  s->frame_exp_strategy[ch] = expstr;
88  }
89 }
90 
91 
92 
94 {
95  int ch, blk;
96  int first_cpl_coords[AC3_MAX_CHANNELS];
97 
98  /* set first cpl coords */
99  for (ch = 1; ch <= s->fbw_channels; ch++)
100  first_cpl_coords[ch] = 1;
101  for (blk = 0; blk < s->num_blocks; blk++) {
102  AC3Block *block = &s->blocks[blk];
103  for (ch = 1; ch <= s->fbw_channels; ch++) {
104  if (block->channel_in_cpl[ch]) {
105  if (first_cpl_coords[ch]) {
106  block->new_cpl_coords[ch] = 2;
107  first_cpl_coords[ch] = 0;
108  }
109  } else {
110  first_cpl_coords[ch] = 1;
111  }
112  }
113  }
114 
115  /* set first cpl leak */
116  for (blk = 0; blk < s->num_blocks; blk++) {
117  AC3Block *block = &s->blocks[blk];
118  if (block->cpl_in_use) {
119  block->new_cpl_leak = 2;
120  break;
121  }
122  }
123 }
124 
125 
127 {
128  int blk, ch;
129  AC3EncOptions *opt = &s->options;
130 
131  put_bits(&s->pb, 16, 0x0b77); /* sync word */
132 
133  /* BSI header */
134  put_bits(&s->pb, 2, 0); /* stream type = independent */
135  put_bits(&s->pb, 3, 0); /* substream id = 0 */
136  put_bits(&s->pb, 11, (s->frame_size / 2) - 1); /* frame size */
137  if (s->bit_alloc.sr_shift) {
138  put_bits(&s->pb, 2, 0x3); /* fscod2 */
139  put_bits(&s->pb, 2, s->bit_alloc.sr_code); /* sample rate code */
140  } else {
141  put_bits(&s->pb, 2, s->bit_alloc.sr_code); /* sample rate code */
142  put_bits(&s->pb, 2, s->num_blks_code); /* number of blocks */
143  }
144  put_bits(&s->pb, 3, s->channel_mode); /* audio coding mode */
145  put_bits(&s->pb, 1, s->lfe_on); /* LFE channel indicator */
146  put_bits(&s->pb, 5, s->bitstream_id); /* bitstream id (EAC3=16) */
147  put_bits(&s->pb, 5, -opt->dialogue_level); /* dialogue normalization level */
148  put_bits(&s->pb, 1, 0); /* no compression gain */
149  /* mixing metadata*/
150  put_bits(&s->pb, 1, opt->eac3_mixing_metadata);
151  if (opt->eac3_mixing_metadata) {
153  put_bits(&s->pb, 2, opt->preferred_stereo_downmix);
154  if (s->has_center) {
155  put_bits(&s->pb, 3, s->ltrt_center_mix_level);
156  put_bits(&s->pb, 3, s->loro_center_mix_level);
157  }
158  if (s->has_surround) {
159  put_bits(&s->pb, 3, s->ltrt_surround_mix_level);
160  put_bits(&s->pb, 3, s->loro_surround_mix_level);
161  }
162  if (s->lfe_on)
163  put_bits(&s->pb, 1, 0);
164  put_bits(&s->pb, 1, 0); /* no program scale */
165  put_bits(&s->pb, 1, 0); /* no ext program scale */
166  put_bits(&s->pb, 2, 0); /* no mixing parameters */
168  put_bits(&s->pb, 1, 0); /* no pan info */
169  put_bits(&s->pb, 1, 0); /* no frame mix config info */
170  }
171  /* info metadata*/
172  put_bits(&s->pb, 1, opt->eac3_info_metadata);
173  if (opt->eac3_info_metadata) {
174  put_bits(&s->pb, 3, s->bitstream_mode);
175  put_bits(&s->pb, 1, opt->copyright);
176  put_bits(&s->pb, 1, opt->original);
177  if (s->channel_mode == AC3_CHMODE_STEREO) {
178  put_bits(&s->pb, 2, opt->dolby_surround_mode);
179  put_bits(&s->pb, 2, opt->dolby_headphone_mode);
180  }
181  if (s->channel_mode >= AC3_CHMODE_2F2R)
182  put_bits(&s->pb, 2, opt->dolby_surround_ex_mode);
183  put_bits(&s->pb, 1, opt->audio_production_info);
184  if (opt->audio_production_info) {
185  put_bits(&s->pb, 5, opt->mixing_level - 80);
186  put_bits(&s->pb, 2, opt->room_type);
187  put_bits(&s->pb, 1, opt->ad_converter_type);
188  }
189  put_bits(&s->pb, 1, 0);
190  }
191  if (s->num_blocks != 6)
192  put_bits(&s->pb, 1, !(s->avctx->frame_number % 6)); /* converter sync flag */
193  put_bits(&s->pb, 1, 0); /* no additional bit stream info */
194 
195  /* frame header */
196  if (s->num_blocks == 6) {
197  put_bits(&s->pb, 1, !s->use_frame_exp_strategy);/* exponent strategy syntax */
198  put_bits(&s->pb, 1, 0); /* aht enabled = no */
199  }
200  put_bits(&s->pb, 2, 0); /* snr offset strategy = 1 */
201  put_bits(&s->pb, 1, 0); /* transient pre-noise processing enabled = no */
202  put_bits(&s->pb, 1, 0); /* block switch syntax enabled = no */
203  put_bits(&s->pb, 1, 0); /* dither flag syntax enabled = no */
204  put_bits(&s->pb, 1, 0); /* bit allocation model syntax enabled = no */
205  put_bits(&s->pb, 1, 0); /* fast gain codes enabled = no */
206  put_bits(&s->pb, 1, 0); /* dba syntax enabled = no */
207  put_bits(&s->pb, 1, 0); /* skip field syntax enabled = no */
208  put_bits(&s->pb, 1, 0); /* spx enabled = no */
209  /* coupling strategy use flags */
210  if (s->channel_mode > AC3_CHMODE_MONO) {
211  put_bits(&s->pb, 1, s->blocks[0].cpl_in_use);
212  for (blk = 1; blk < s->num_blocks; blk++) {
213  AC3Block *block = &s->blocks[blk];
214  put_bits(&s->pb, 1, block->new_cpl_strategy);
215  if (block->new_cpl_strategy)
216  put_bits(&s->pb, 1, block->cpl_in_use);
217  }
218  }
219  /* exponent strategy */
220  if (s->use_frame_exp_strategy) {
221  for (ch = !s->cpl_on; ch <= s->fbw_channels; ch++)
222  put_bits(&s->pb, 5, s->frame_exp_strategy[ch]);
223  } else {
224  for (blk = 0; blk < s->num_blocks; blk++)
225  for (ch = !s->blocks[blk].cpl_in_use; ch <= s->fbw_channels; ch++)
226  put_bits(&s->pb, 2, s->exp_strategy[ch][blk]);
227  }
228  if (s->lfe_on) {
229  for (blk = 0; blk < s->num_blocks; blk++)
230  put_bits(&s->pb, 1, s->exp_strategy[s->lfe_channel][blk]);
231  }
232  /* E-AC-3 to AC-3 converter exponent strategy (not optional when num blocks == 6) */
233  if (s->num_blocks != 6) {
234  put_bits(&s->pb, 1, 0);
235  } else {
236  for (ch = 1; ch <= s->fbw_channels; ch++) {
237  if (s->use_frame_exp_strategy)
238  put_bits(&s->pb, 5, s->frame_exp_strategy[ch]);
239  else
240  put_bits(&s->pb, 5, 0);
241  }
242  }
243  /* snr offsets */
244  put_bits(&s->pb, 6, s->coarse_snr_offset);
245  put_bits(&s->pb, 4, s->fine_snr_offset[1]);
246  /* block start info */
247  if (s->num_blocks > 1)
248  put_bits(&s->pb, 1, 0);
249 }
250 
251 
252 #if CONFIG_EAC3_ENCODER
253 AVCodec ff_eac3_encoder = {
254  .name = "eac3",
255  .type = AVMEDIA_TYPE_AUDIO,
256  .id = AV_CODEC_ID_EAC3,
257  .priv_data_size = sizeof(AC3EncodeContext),
259  .encode2 = ff_ac3_float_encode_frame,
261  .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLTP,
263  .long_name = NULL_IF_CONFIG_SMALL("ATSC A/52 E-AC-3"),
264  .priv_class = &eac3enc_class,
265  .channel_layouts = ff_ac3_channel_layouts,
266  .defaults = ac3_defaults,
267 };
268 #endif
uint8_t exp_strategy[AC3_MAX_CHANNELS][AC3_MAX_BLOCKS]
exponent strategies
Definition: ac3enc.h:245
int eac3_mixing_metadata
Definition: ac3enc.h:115
const char * s
Definition: avisynth_c.h:668
int dialogue_level
Definition: ac3enc.h:95
struct AC3EncodeContext AC3EncodeContext
AC-3 encoder private context.
Encoding Options used by AVOption.
Definition: ac3enc.h:93
av_default_item_name
int dolby_surround_ex_mode
Definition: ac3enc.h:112
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
static const AVClass eac3enc_class
Definition: eac3enc.c:36
void ff_eac3_set_cpl_states(AC3EncodeContext *s)
Set coupling states.
Definition: eac3enc.c:93
PutBitContext pb
bitstream writer context
Definition: ac3enc.h:161
#define blk(i)
Definition: sha.c:169
AC3BitAllocParameters bit_alloc
bit allocation parameters
Definition: ac3enc.h:222
int ff_ac3_float_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
int new_cpl_leak
send new coupling leak info
Definition: ac3enc.h:150
void ff_eac3_get_frame_exp_strategy(AC3EncodeContext *s)
Determine frame exponent strategy use and indices.
Definition: eac3enc.c:66
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:55
int channel_mode
channel mode (acmod)
Definition: ac3enc.h:193
int fbw_channels
number of full-bandwidth channels (nfchans)
Definition: ac3enc.h:187
uint8_t new_cpl_coords[AC3_MAX_CHANNELS]
send new coupling coordinates (cplcoe)
Definition: ac3enc.h:147
av_cold int ff_ac3_encode_close(AVCodecContext *avctx)
Finalize encoding and free any memory allocated by the encoder.
Definition: ac3enc.c:2015
int loro_center_mix_level
Lo/Ro center mix level code.
Definition: ac3enc.h:200
int lfe_channel
channel index of the LFE channel
Definition: ac3enc.h:190
float, planar
Definition: samplefmt.h:60
static int8_t eac3_frame_expstr_index_tab[3][4][4][4][4][4]
LUT for finding a matching frame exponent strategy index from a set of exponent strategies for a sing...
Definition: eac3enc.c:47
int loro_surround_mix_level
Lo/Ro surround mix level code.
Definition: ac3enc.h:201
uint8_t channel_in_cpl[AC3_MAX_CHANNELS]
channel in coupling (chincpl)
Definition: ac3enc.h:145
int eac3_info_metadata
Definition: ac3enc.h:116
int mixing_level
Definition: ac3enc.h:101
int num_blks_code
number of blocks code (numblkscod)
Definition: ac3enc.h:178
const uint64_t ff_ac3_channel_layouts[19]
List of supported channel layouts.
Definition: ac3enc.c:79
AC3EncOptions options
encoding options
Definition: ac3enc.h:159
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
#define AC3_MAX_CHANNELS
maximum number of channels, including coupling channel
Definition: ac3.h:31
int cpl_on
coupling turned on for this frame
Definition: ac3enc.h:208
const char * name
Name of the codec implementation.
int ltrt_surround_mix_level
Lt/Rt surround mix level code.
Definition: ac3enc.h:199
int new_cpl_strategy
send new coupling strategy
Definition: ac3enc.h:143
static void put_bits(J2kEncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:160
int cpl_in_use
coupling in use for this block (cplinu)
Definition: ac3enc.h:144
Data for a single audio block.
Definition: ac3enc.h:128
int bitstream_mode
bitstream mode (bsmod)
Definition: ac3enc.h:173
int has_surround
indicates if there are one or more surround channels
Definition: ac3enc.h:192
uint8_t frame_exp_strategy[AC3_MAX_CHANNELS]
frame exp strategy index
Definition: ac3enc.h:246
int ad_converter_type
Definition: ac3enc.h:114
void ff_eac3_exponent_init(void)
Initialize E-AC-3 exponent tables.
Definition: eac3enc.c:50
int coarse_snr_offset
coarse SNR offsets (csnroffst)
Definition: ac3enc.h:223
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
void ff_eac3_output_frame_header(AC3EncodeContext *s)
Write the E-AC-3 frame header to the output bitstream.
Definition: eac3enc.c:126
int audio_production_info
Definition: ac3enc.h:100
int dolby_surround_mode
Definition: ac3enc.h:99
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:375
int has_center
indicates if there is a center channel
Definition: ac3enc.h:191
Describe the class of an AVClass context structure.
Definition: log.h:50
synthesis window for stochastic i
int ltrt_center_mix_level
Lt/Rt center mix level code.
Definition: ac3enc.h:198
static const AVCodecDefault ac3_defaults[]
AC-3 encoder private context.
Definition: ac3enc.h:157
AC3Block blocks[AC3_MAX_BLOCKS]
per-block info
Definition: ac3enc.h:168
int preferred_stereo_downmix
Definition: ac3enc.h:106
int num_blocks
number of blocks per frame
Definition: ac3enc.h:179
int frame_size
current frame size in bytes
Definition: ac3enc.h:181
int room_type
Definition: ac3enc.h:102
const uint8_t ff_eac3_frm_expstr[32][6]
Table E2.14 Frame Exponent Strategy Combinations.
Definition: eac3_data.c:1062
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:49
static const AVOption ac3_options[]
int bitstream_id
bitstream id (bsid)
Definition: ac3enc.h:172
int dolby_headphone_mode
Definition: ac3enc.h:113
AVCodecContext * avctx
parent AVCodecContext
Definition: ac3enc.h:160
int original
Definition: ac3enc.h:104
AC-3 encoder & E-AC-3 encoder common header.
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:700
int frame_number
Frame counter, set by libavcodec.
int use_frame_exp_strategy
indicates use of frame exp strategy
Definition: ac3enc.h:247
E-AC-3 encoder.
int lfe_on
indicates if there is an LFE channel (lfeon)
Definition: ac3enc.h:189
int fine_snr_offset[AC3_MAX_CHANNELS]
fine SNR offsets (fsnroffst)
Definition: ac3enc.h:225
int copyright
Definition: ac3enc.h:103
av_cold int ff_ac3_encode_init(AVCodecContext *avctx)
Definition: ac3enc.c:2419