Git init
[framework/multimedia/ffmpeg.git] / libavcodec / eac3enc.c
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
31
32 #define AC3ENC_TYPE AC3ENC_TYPE_EAC3
33 #include "ac3enc_opts_template.c"
34 static AVClass eac3enc_class = { "E-AC-3 Encoder", av_default_item_name,
35                                  eac3_options, LIBAVUTIL_VERSION_INT };
36
37
38 void ff_eac3_set_cpl_states(AC3EncodeContext *s)
39 {
40     int ch, blk;
41     int first_cpl_coords[AC3_MAX_CHANNELS];
42
43     /* set first cpl coords */
44     for (ch = 1; ch <= s->fbw_channels; ch++)
45         first_cpl_coords[ch] = 1;
46     for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
47         AC3Block *block = &s->blocks[blk];
48         for (ch = 1; ch <= s->fbw_channels; ch++) {
49             if (block->channel_in_cpl[ch]) {
50                 if (first_cpl_coords[ch]) {
51                     block->new_cpl_coords = 2;
52                     first_cpl_coords[ch]  = 0;
53                 }
54             } else {
55                 first_cpl_coords[ch] = 1;
56             }
57         }
58     }
59
60     /* set first cpl leak */
61     for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
62         AC3Block *block = &s->blocks[blk];
63         if (block->cpl_in_use) {
64             block->new_cpl_leak = 2;
65             break;
66         }
67     }
68 }
69
70
71 void ff_eac3_output_frame_header(AC3EncodeContext *s)
72 {
73     int blk, ch;
74     AC3EncOptions *opt = &s->options;
75
76     put_bits(&s->pb, 16, 0x0b77);                   /* sync word */
77
78     /* BSI header */
79     put_bits(&s->pb,  2, 0);                        /* stream type = independent */
80     put_bits(&s->pb,  3, 0);                        /* substream id = 0 */
81     put_bits(&s->pb, 11, (s->frame_size / 2) - 1);  /* frame size */
82     if (s->bit_alloc.sr_shift) {
83         put_bits(&s->pb, 2, 0x3);                   /* fscod2 */
84         put_bits(&s->pb, 2, s->bit_alloc.sr_code);  /* sample rate code */
85     } else {
86         put_bits(&s->pb, 2, s->bit_alloc.sr_code);  /* sample rate code */
87         put_bits(&s->pb, 2, 0x3);                   /* number of blocks = 6 */
88     }
89     put_bits(&s->pb, 3, s->channel_mode);           /* audio coding mode */
90     put_bits(&s->pb, 1, s->lfe_on);                 /* LFE channel indicator */
91     put_bits(&s->pb, 5, s->bitstream_id);           /* bitstream id (EAC3=16) */
92     put_bits(&s->pb, 5, -opt->dialogue_level);      /* dialogue normalization level */
93     put_bits(&s->pb, 1, 0);                         /* no compression gain */
94     put_bits(&s->pb, 1, 0);                         /* no mixing metadata */
95     /* TODO: mixing metadata */
96     put_bits(&s->pb, 1, 0);                         /* no info metadata */
97     /* TODO: info metadata */
98     put_bits(&s->pb, 1, 0);                         /* no additional bit stream info */
99
100     /* frame header */
101     put_bits(&s->pb, 1, 1);                         /* exponent strategy syntax = each block */
102     put_bits(&s->pb, 1, 0);                         /* aht enabled = no */
103     put_bits(&s->pb, 2, 0);                         /* snr offset strategy = 1 */
104     put_bits(&s->pb, 1, 0);                         /* transient pre-noise processing enabled = no */
105     put_bits(&s->pb, 1, 0);                         /* block switch syntax enabled = no */
106     put_bits(&s->pb, 1, 0);                         /* dither flag syntax enabled = no */
107     put_bits(&s->pb, 1, 0);                         /* bit allocation model syntax enabled = no */
108     put_bits(&s->pb, 1, 0);                         /* fast gain codes enabled = no */
109     put_bits(&s->pb, 1, 0);                         /* dba syntax enabled = no */
110     put_bits(&s->pb, 1, 0);                         /* skip field syntax enabled = no */
111     put_bits(&s->pb, 1, 0);                         /* spx enabled = no */
112     /* coupling strategy use flags */
113     if (s->channel_mode > AC3_CHMODE_MONO) {
114         put_bits(&s->pb, 1, s->blocks[0].cpl_in_use);
115         for (blk = 1; blk < AC3_MAX_BLOCKS; blk++) {
116             AC3Block *block = &s->blocks[blk];
117             put_bits(&s->pb, 1, block->new_cpl_strategy);
118             if (block->new_cpl_strategy)
119                 put_bits(&s->pb, 1, block->cpl_in_use);
120         }
121     }
122     /* exponent strategy */
123     for (blk = 0; blk < AC3_MAX_BLOCKS; blk++)
124         for (ch = !s->blocks[blk].cpl_in_use; ch <= s->fbw_channels; ch++)
125             put_bits(&s->pb, 2, s->exp_strategy[ch][blk]);
126     if (s->lfe_on) {
127         for (blk = 0; blk < AC3_MAX_BLOCKS; blk++)
128             put_bits(&s->pb, 1, s->exp_strategy[s->lfe_channel][blk]);
129     }
130     /* E-AC-3 to AC-3 converter exponent strategy (unfortunately not optional...) */
131     for (ch = 1; ch <= s->fbw_channels; ch++)
132         put_bits(&s->pb, 5, 0);
133     /* snr offsets */
134     put_bits(&s->pb, 6, s->coarse_snr_offset);
135     put_bits(&s->pb, 4, s->fine_snr_offset[1]);
136     /* block start info */
137     put_bits(&s->pb, 1, 0);
138 }
139
140
141 #if CONFIG_EAC3_ENCODER
142 AVCodec ff_eac3_encoder = {
143     .name            = "eac3",
144     .type            = AVMEDIA_TYPE_AUDIO,
145     .id              = CODEC_ID_EAC3,
146     .priv_data_size  = sizeof(AC3EncodeContext),
147     .init            = ff_ac3_encode_init,
148     .encode          = ff_ac3_encode_frame,
149     .close           = ff_ac3_encode_close,
150     .sample_fmts     = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_FLT,AV_SAMPLE_FMT_NONE},
151     .long_name       = NULL_IF_CONFIG_SMALL("ATSC A/52 E-AC-3"),
152     .priv_class      = &eac3enc_class,
153     .channel_layouts = ff_ac3_channel_layouts,
154 };
155 #endif