FFmpeg
Loading...
Searching...
No Matches
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 FFmpeg.
6 *
7 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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 AC3ENC_FLOAT 1
28
30#include "libavutil/thread.h"
31#include "ac3enc.h"
32#include "codec_internal.h"
33#include "eac3enc.h"
34#include "eac3_data.h"
35#include "put_bits.h"
36
37
38static const AVClass eac3enc_class = {
39 .class_name = "E-AC-3 Encoder",
40 .item_name = av_default_item_name,
41 .option = &ff_ac3_enc_options[2], /* First two options are AC-3 only. */
42 .version = LIBAVUTIL_VERSION_INT,
43};
44
45/**
46 * LUT for finding a matching frame exponent strategy index from a set of
47 * exponent strategies for a single channel across all 6 blocks.
48 */
49static int8_t eac3_frame_expstr_index_tab[3][4][4][4][4][4];
50
51
52/**
53 * Initialize E-AC-3 exponent tables.
54 */
55static av_cold void eac3_exponent_init(void)
56{
57 int i;
58
60 for (i = 0; i < 32; i++) {
66 [ff_eac3_frm_expstr[i][5]] = i;
67 }
68}
69
70
72{
73 int ch;
74
75 if (s->num_blocks < 6) {
76 s->use_frame_exp_strategy = 0;
77 return;
78 }
79
80 s->use_frame_exp_strategy = 1;
81 for (ch = !s->cpl_on; ch <= s->fbw_channels; ch++) {
82 int expstr = eac3_frame_expstr_index_tab[s->exp_strategy[ch][0]-1]
83 [s->exp_strategy[ch][1]]
84 [s->exp_strategy[ch][2]]
85 [s->exp_strategy[ch][3]]
86 [s->exp_strategy[ch][4]]
87 [s->exp_strategy[ch][5]];
88 if (expstr < 0) {
89 s->use_frame_exp_strategy = 0;
90 break;
91 }
92 s->frame_exp_strategy[ch] = expstr;
93 }
94}
95
96
97
99{
100 int ch, blk;
101 int first_cpl_coords[AC3_MAX_CHANNELS];
102
103 /* set first cpl coords */
104 for (ch = 1; ch <= s->fbw_channels; ch++)
105 first_cpl_coords[ch] = 1;
106 for (blk = 0; blk < s->num_blocks; blk++) {
107 AC3Block *block = &s->blocks[blk];
108 for (ch = 1; ch <= s->fbw_channels; ch++) {
109 if (block->channel_in_cpl[ch]) {
110 if (first_cpl_coords[ch]) {
111 block->new_cpl_coords[ch] = 2;
112 first_cpl_coords[ch] = 0;
113 }
114 } else {
115 first_cpl_coords[ch] = 1;
116 }
117 }
118 }
119
120 /* set first cpl leak */
121 for (blk = 0; blk < s->num_blocks; blk++) {
122 AC3Block *block = &s->blocks[blk];
123 if (block->cpl_in_use) {
124 block->new_cpl_leak = 2;
125 break;
126 }
127 }
128}
129
130/**
131 * Write the E-AC-3 frame header to the output bitstream.
132 */
134{
135 int blk, ch;
136 AC3EncOptions *opt = &s->options;
137
139
140 put_bits(pb, 16, 0x0b77); /* sync word */
141
142 /* BSI header */
143 put_bits(pb, 2, 0); /* stream type = independent */
144 put_bits(pb, 3, 0); /* substream id = 0 */
145 put_bits(pb, 11, (s->frame_size / 2) - 1); /* frame size */
146 put_bits(pb, 2, s->bit_alloc.sr_code); /* sample rate code */
147 put_bits(pb, 2, s->num_blks_code); /* number of blocks */
148 put_bits(pb, 3, s->channel_mode); /* audio coding mode */
149 put_bits(pb, 1, s->lfe_on); /* LFE channel indicator */
150 put_bits(pb, 5, s->bitstream_id); /* bitstream id (EAC3=16) */
151 put_bits(pb, 5, -opt->dialogue_level); /* dialogue normalization level */
152 put_bits(pb, 1, 0); /* no compression gain */
153 /* mixing metadata*/
154 put_bits(pb, 1, opt->eac3_mixing_metadata);
155 if (opt->eac3_mixing_metadata) {
156 if (s->channel_mode > AC3_CHMODE_STEREO)
158 if (s->has_center) {
159 put_bits(pb, 3, s->ltrt_center_mix_level);
160 put_bits(pb, 3, s->loro_center_mix_level);
161 }
162 if (s->has_surround) {
163 put_bits(pb, 3, s->ltrt_surround_mix_level);
164 put_bits(pb, 3, s->loro_surround_mix_level);
165 }
166 if (s->lfe_on)
167 put_bits(pb, 1, 0);
168 put_bits(pb, 1, 0); /* no program scale */
169 put_bits(pb, 1, 0); /* no ext program scale */
170 put_bits(pb, 2, 0); /* no mixing parameters */
171 if (s->channel_mode < AC3_CHMODE_STEREO)
172 put_bits(pb, 1, 0); /* no pan info */
173 put_bits(pb, 1, 0); /* no frame mix config info */
174 }
175 /* info metadata*/
176 put_bits(pb, 1, opt->eac3_info_metadata);
177 if (opt->eac3_info_metadata) {
178 put_bits(pb, 3, s->bitstream_mode);
179 put_bits(pb, 1, opt->copyright);
180 put_bits(pb, 1, opt->original);
181 if (s->channel_mode == AC3_CHMODE_STEREO) {
182 put_bits(pb, 2, opt->dolby_surround_mode);
183 put_bits(pb, 2, opt->dolby_headphone_mode);
184 }
185 if (s->channel_mode >= AC3_CHMODE_2F2R)
186 put_bits(pb, 2, opt->dolby_surround_ex_mode);
187 put_bits(pb, 1, opt->audio_production_info);
188 if (opt->audio_production_info) {
189 put_bits(pb, 5, opt->mixing_level - 80);
190 put_bits(pb, 2, opt->room_type);
191 put_bits(pb, 1, opt->ad_converter_type);
192 }
193 put_bits(pb, 1, 0);
194 }
195 if (s->num_blocks != 6)
196 put_bits(pb, 1, !(s->avctx->frame_num % 6)); /* converter sync flag */
197 put_bits(pb, 1, 0); /* no additional bit stream info */
198
199 /* frame header */
200 if (s->num_blocks == 6) {
201 put_bits(pb, 1, !s->use_frame_exp_strategy); /* exponent strategy syntax */
202 put_bits(pb, 1, 0); /* aht enabled = no */
203 }
204 put_bits(pb, 2, 0); /* snr offset strategy = 1 */
205 put_bits(pb, 1, 0); /* transient pre-noise processing enabled = no */
206 put_bits(pb, 1, 0); /* block switch syntax enabled = no */
207 put_bits(pb, 1, 0); /* dither flag syntax enabled = no */
208 put_bits(pb, 1, 0); /* bit allocation model syntax enabled = no */
209 put_bits(pb, 1, 0); /* fast gain codes enabled = no */
210 put_bits(pb, 1, 0); /* dba syntax enabled = no */
211 put_bits(pb, 1, 0); /* skip field syntax enabled = no */
212 put_bits(pb, 1, 0); /* spx enabled = no */
213 /* coupling strategy use flags */
214 if (s->channel_mode > AC3_CHMODE_MONO) {
215 put_bits(pb, 1, s->blocks[0].cpl_in_use);
216 for (blk = 1; blk < s->num_blocks; blk++) {
217 AC3Block *block = &s->blocks[blk];
218 put_bits(pb, 1, block->new_cpl_strategy);
219 if (block->new_cpl_strategy)
220 put_bits(pb, 1, block->cpl_in_use);
221 }
222 }
223 /* exponent strategy */
224 if (s->use_frame_exp_strategy) {
225 for (ch = !s->cpl_on; ch <= s->fbw_channels; ch++)
226 put_bits(pb, 5, s->frame_exp_strategy[ch]);
227 } else {
228 for (blk = 0; blk < s->num_blocks; blk++)
229 for (ch = !s->blocks[blk].cpl_in_use; ch <= s->fbw_channels; ch++)
230 put_bits(pb, 2, s->exp_strategy[ch][blk]);
231 }
232 if (s->lfe_on) {
233 for (blk = 0; blk < s->num_blocks; blk++)
234 put_bits(pb, 1, s->exp_strategy[s->lfe_channel][blk]);
235 }
236 /* E-AC-3 to AC-3 converter exponent strategy (not optional when num blocks == 6) */
237 if (s->num_blocks != 6) {
238 put_bits(pb, 1, 0);
239 } else {
240 for (ch = 1; ch <= s->fbw_channels; ch++) {
241 if (s->use_frame_exp_strategy)
242 put_bits(pb, 5, s->frame_exp_strategy[ch]);
243 else
244 put_bits(pb, 5, 0);
245 }
246 }
247 /* snr offsets */
248 put_bits(pb, 6, s->coarse_snr_offset);
249 put_bits(pb, 4, s->fine_snr_offset[1]);
250 /* block start info */
251 if (s->num_blocks > 1)
252 put_bits(pb, 1, 0);
253}
254
256{
257 static AVOnce init_static_once = AV_ONCE_INIT;
258 AC3EncodeContext *s = avctx->priv_data;
259
260 s->eac3 = 1;
261 s->output_frame_header = eac3_output_frame_header;
262
263 ff_thread_once(&init_static_once, eac3_exponent_init);
264
265 return ff_ac3_float_encode_init(avctx);
266}
267
#define AC3_MAX_CHANNELS
maximum number of channels, including coupling channel
Definition ac3defs.h:26
@ AC3_CHMODE_MONO
Definition ac3defs.h:69
@ AC3_CHMODE_STEREO
Definition ac3defs.h:70
@ AC3_CHMODE_2F2R
Definition ac3defs.h:74
av_cold int ff_ac3_encode_close(AVCodecContext *avctx)
Finalize encoding and free any memory allocated by the encoder.
Definition ac3enc.c:2165
const AVOption ff_ac3_enc_options[]
Definition ac3enc.c:80
const AVChannelLayout ff_ac3_ch_layouts[19]
List of supported channel layouts.
Definition ac3enc.c:152
const FFCodecDefault ff_ac3_enc_defaults[]
Definition ac3enc.c:137
int ff_ac3_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition ac3enc.c:1981
AC-3 encoder & E-AC-3 encoder common header.
int ff_ac3_float_encode_init(AVCodecContext *avctx)
const int ff_ac3_sample_rate_tab[]
Definition ac3tab.c:96
const FFCodec ff_eac3_encoder
Definition eac3enc.c:268
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define CODEC_SAMPLERATES_ARRAY(array)
#define FF_CODEC_ENCODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define CODEC_SAMPLEFMTS(...)
#define CODEC_CH_LAYOUTS_ARRAY(array)
static int16_t block[64]
Definition dct.c:125
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
const uint8_t ff_eac3_frm_expstr[32][6]
Table E2.14 Frame Exponent Strategy Combinations.
Definition eac3_data.c:1064
static void eac3_output_frame_header(AC3EncodeContext *s, PutBitContext *pb)
Write the E-AC-3 frame header to the output bitstream.
Definition eac3enc.c:133
static av_cold int eac3_encode_init(AVCodecContext *avctx)
Definition eac3enc.c:255
void ff_eac3_set_cpl_states(AC3EncodeContext *s)
Set coupling states.
Definition eac3enc.c:98
static av_cold void eac3_exponent_init(void)
Initialize E-AC-3 exponent tables.
Definition eac3enc.c:55
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:49
void ff_eac3_get_frame_exp_strategy(AC3EncodeContext *s)
Determine frame exponent strategy use and indices.
Definition eac3enc.c:71
static const AVClass eac3enc_class
Definition eac3enc.c:38
E-AC-3 encoder.
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition codec.h:147
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition codec.h:79
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
Definition codec.h:84
@ AV_CODEC_ID_EAC3
Definition codec_id.h:493
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition samplefmt.h:66
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition j2kenc.c:154
Macro definitions for various function/variable attributes.
#define av_cold
Definition attributes.h:117
#define AVOnce
Definition thread.h:202
static int ff_thread_once(char *control, void(*routine)(void))
Definition thread.h:205
#define AV_ONCE_INIT
Definition thread.h:203
bitstream writer API
static void put_bits_assume_flushed(const PutBitContext *s)
Inform the compiler that a PutBitContext is flushed (i.e.
Definition put_bits.h:82
#define blk(i)
Definition sha.c:55
Data for a single audio block.
Definition ac3enc.h:129
Encoding Options used by AVOption.
Definition ac3enc.h:94
int dialogue_level
Definition ac3enc.h:96
int dolby_surround_ex_mode
Definition ac3enc.h:113
int eac3_mixing_metadata
Definition ac3enc.h:116
int dolby_headphone_mode
Definition ac3enc.h:114
int audio_production_info
Definition ac3enc.h:101
int dolby_surround_mode
Definition ac3enc.h:100
int room_type
Definition ac3enc.h:103
int copyright
Definition ac3enc.h:104
int eac3_info_metadata
Definition ac3enc.h:117
int ad_converter_type
Definition ac3enc.h:115
int preferred_stereo_downmix
Definition ac3enc.h:107
int mixing_level
Definition ac3enc.h:102
AC-3 encoder private context.
Definition ac3enc.h:159
Describe the class of an AVClass context structure.
Definition log.h:76
main external API structure.
Definition avcodec.h:443
void * priv_data
Definition avcodec.h:470