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