2 * Copyright (C) 2013 nu774
3 * For conditions of distribution and use, see copyright notice in COPYING
16 int aacenc_is_sbr_ratio_available()
18 #if AACENCODER_LIB_VL0 < 3 || (AACENCODER_LIB_VL0==3 && AACENCODER_LIB_VL1<4)
22 aacenc_get_lib_info(&lib_info
);
23 return lib_info
.version
> 0x03040000;
27 int aacenc_is_sbr_active(const aacenc_param_t
*params
)
29 switch (params
->profile
) {
30 case AOT_SBR
: case AOT_PS
: case AOT_MP2_SBR
: case AOT_MP2_PS
:
31 case AOT_DABPLUS_SBR
: case AOT_DABPLUS_PS
:
32 case AOT_DRM_SBR
: case AOT_DRM_MPEG_PS
:
35 if (params
->profile
== AOT_ER_AAC_ELD
&& params
->lowdelay_sbr
)
40 int aacenc_is_dual_rate_sbr(const aacenc_param_t
*params
)
42 if (params
->profile
== AOT_PS
|| params
->profile
== AOT_MP2_PS
)
44 else if (params
->profile
== AOT_SBR
|| params
->profile
== AOT_MP2_SBR
)
45 return params
->sbr_ratio
== 0 || params
->sbr_ratio
== 2;
46 else if (params
->profile
== AOT_ER_AAC_ELD
&& params
->lowdelay_sbr
)
47 return params
->sbr_ratio
== 2;
51 void aacenc_get_lib_info(LIB_INFO
*info
)
53 LIB_INFO
*lib_info
= 0;
54 lib_info
= calloc(FDK_MODULE_LAST
, sizeof(LIB_INFO
));
55 if (aacEncGetLibInfo(lib_info
) == AACENC_OK
) {
57 for (i
= 0; i
< FDK_MODULE_LAST
; ++i
) {
58 if (lib_info
[i
].module_id
== FDK_AACENC
) {
59 memcpy(info
, &lib_info
[i
], sizeof(LIB_INFO
));
67 static const unsigned aacenc_sampling_freq_tab
[] = {
68 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
69 16000, 12000, 11025, 8000, 7350, 0, 0, 0
73 unsigned sampling_freq_index(unsigned rate
)
76 for (i
= 0; aacenc_sampling_freq_tab
[i
]; ++i
)
77 if (aacenc_sampling_freq_tab
[i
] == rate
)
83 * Append backward compatible SBR/PS signaling to implicit signaling ASC,
84 * if SBR/PS is present.
86 int aacenc_mp4asc(const aacenc_param_t
*params
,
87 const uint8_t *asc
, uint32_t ascsize
,
88 uint8_t *outasc
, uint32_t *outsize
)
90 unsigned asc_sfreq
= aacenc_sampling_freq_tab
[(asc
[0]&0x7)<<1 |asc
[1]>>7];
91 unsigned shift
= aacenc_is_dual_rate_sbr(params
);
93 switch (params
->profile
) {
98 if (*outsize
< ascsize
+ 3)
100 memcpy(outasc
, asc
, ascsize
);
101 /* syncExtensionType:11 (value:0x2b7) */
102 outasc
[ascsize
+0] = 0x2b << 1;
103 outasc
[ascsize
+1] = 0x7 << 5;
104 /* extensionAudioObjectType:5 (value:5)*/
105 outasc
[ascsize
+1] |= 5;
106 /* sbrPresentFlag:1 (value:1) */
107 outasc
[ascsize
+2] = 0x80;
108 /* extensionSamplingFrequencyIndex:4 */
109 outasc
[ascsize
+2] |= sampling_freq_index(asc_sfreq
<< shift
) << 3;
110 if (params
->profile
== AOT_SBR
) {
111 *outsize
= ascsize
+ 3;
114 if (*outsize
< ascsize
+ 5)
116 /* syncExtensionType:11 (value:0x548) */
117 outasc
[ascsize
+2] |= 0x5;
118 outasc
[ascsize
+3] = 0x48;
119 /* psPresentFlag:1 (value:1) */
120 outasc
[ascsize
+4] = 0x80;
121 *outsize
= ascsize
+ 5;
124 if (*outsize
< ascsize
)
126 memcpy(outasc
, asc
, ascsize
);
132 int aacenc_channel_mode(const pcm_sample_description_t
*format
)
134 uint32_t chanmask
= format
->channel_mask
;
136 if (format
->channels_per_frame
> 8)
139 static uint32_t defaults
[] = { 0x4, 0x3, 0x7, 0, 0x37, 0x3f, 0, 0x63f };
140 chanmask
= defaults
[format
->channels_per_frame
- 1];
143 case 0x3: return MODE_2
;
144 case 0x4: return MODE_1
;
145 case 0x7: return MODE_1_2
;
146 case 0x37: return MODE_1_2_2
;
147 case 0x3f: return MODE_1_2_2_1
;
148 case 0x107: return MODE_1_2_1
;
149 case 0x607: return MODE_1_2_2
;
150 case 0x60f: return MODE_1_2_2_1
;
151 #if AACENCODER_LIB_VL0 > 3 || (AACENCODER_LIB_VL0==3 && AACENCODER_LIB_VL1>=4)
152 case 0xff: return MODE_1_2_2_2_1
;
153 case 0x63f: return MODE_7_1_REAR_SURROUND
;
159 int aacenc_init(HANDLE_AACENCODER
*encoder
, const aacenc_param_t
*params
,
160 const pcm_sample_description_t
*format
,
161 AACENC_InfoStruct
*info
)
168 aacenc_get_lib_info(&lib_info
);
170 if ((channel_mode
= aacenc_channel_mode(format
)) == 0) {
171 fprintf(stderr
, "ERROR: unsupported channel layout\n");
174 if (aacEncOpen(encoder
, 0, 0) != AACENC_OK
) {
175 fprintf(stderr
, "ERROR: aacEncOpen() failed\n");
178 aot
= (params
->profile
? params
->profile
: AOT_AAC_LC
);
179 if (aacEncoder_SetParam(*encoder
, AACENC_AOT
, aot
) != AACENC_OK
) {
180 fprintf(stderr
, "ERROR: unsupported profile\n");
183 if (params
->bitrate_mode
== 0)
184 aacEncoder_SetParam(*encoder
, AACENC_BITRATE
, params
->bitrate
);
185 else if (aacEncoder_SetParam(*encoder
, AACENC_BITRATEMODE
,
186 params
->bitrate_mode
) != AACENC_OK
) {
187 fprintf(stderr
, "ERROR: unsupported bitrate mode\n");
190 if (aacEncoder_SetParam(*encoder
, AACENC_SAMPLERATE
,
191 format
->sample_rate
) != AACENC_OK
) {
192 fprintf(stderr
, "ERROR: unsupported sample rate\n");
195 if (aacEncoder_SetParam(*encoder
, AACENC_CHANNELMODE
,
196 channel_mode
) != AACENC_OK
) {
197 fprintf(stderr
, "ERROR: unsupported channel mode\n");
200 aacEncoder_SetParam(*encoder
, AACENC_BANDWIDTH
, params
->bandwidth
);
201 aacEncoder_SetParam(*encoder
, AACENC_CHANNELORDER
, 1);
202 aacEncoder_SetParam(*encoder
, AACENC_AFTERBURNER
, !!params
->afterburner
);
204 aacEncoder_SetParam(*encoder
, AACENC_SBR_MODE
, params
->lowdelay_sbr
);
206 #if AACENCODER_LIB_VL0 > 3 || (AACENCODER_LIB_VL0==3 && AACENCODER_LIB_VL1>=4)
207 if (lib_info
.version
> 0x03040000)
208 aacEncoder_SetParam(*encoder
, AACENC_SBR_RATIO
, params
->sbr_ratio
);
211 if (aacEncoder_SetParam(*encoder
, AACENC_TRANSMUX
,
212 params
->transport_format
) != AACENC_OK
) {
213 fprintf(stderr
, "ERROR: unsupported transport format\n");
216 if (aacEncoder_SetParam(*encoder
, AACENC_SIGNALING_MODE
,
217 params
->sbr_signaling
) != AACENC_OK
) {
218 fprintf(stderr
, "ERROR: failed to set SBR signaling mode\n");
221 if (params
->adts_crc_check
)
222 aacEncoder_SetParam(*encoder
, AACENC_PROTECTION
, 1);
223 if (params
->header_period
)
224 aacEncoder_SetParam(*encoder
, AACENC_HEADER_PERIOD
,
225 params
->header_period
);
227 if (aacEncEncode(*encoder
, 0, 0, 0, 0) != AACENC_OK
) {
228 fprintf(stderr
, "ERROR: encoder initialization failed\n");
231 if (aacEncInfo(*encoder
, info
) != AACENC_OK
) {
232 fprintf(stderr
, "ERROR: cannot retrieve encoder info\n");
238 aacEncClose(encoder
);
242 int aac_encode_frame(HANDLE_AACENCODER encoder
,
243 const pcm_sample_description_t
*format
,
244 const int16_t *input
, unsigned iframes
,
245 aacenc_frame_t
*output
)
247 uint32_t ilen
= iframes
* format
->channels_per_frame
;
248 AACENC_BufDesc ibdesc
= { 0 }, obdesc
= { 0 };
249 AACENC_InArgs iargs
= { 0 };
250 AACENC_OutArgs oargs
= { 0 };
251 void *ibufs
[] = { (void*)input
};
253 INT ibuf_ids
[] = { IN_AUDIO_DATA
};
254 INT obuf_ids
[] = { OUT_BITSTREAM_DATA
};
255 INT ibuf_sizes
[] = { ilen
* sizeof(int16_t) };
257 INT ibuf_el_sizes
[] = { sizeof(int16_t) };
258 INT obuf_el_sizes
[] = { 1 };
260 unsigned channel_mode
, obytes
;
262 channel_mode
= aacEncoder_GetParam(encoder
, AACENC_CHANNELMODE
);
263 obytes
= 6144 / 8 * channel_mode
;
264 if (!output
->data
|| output
->capacity
< obytes
) {
265 uint8_t *p
= realloc(output
->data
, obytes
);
267 output
->capacity
= obytes
;
270 obufs
[0] = output
->data
;
271 obuf_sizes
[0] = obytes
;
273 iargs
.numInSamples
= ilen
? ilen
: -1; /* -1 for signaling EOF */
276 ibdesc
.bufferIdentifiers
= ibuf_ids
;
277 ibdesc
.bufSizes
= ibuf_sizes
;
278 ibdesc
.bufElSizes
= ibuf_el_sizes
;
281 obdesc
.bufferIdentifiers
= obuf_ids
;
282 obdesc
.bufSizes
= obuf_sizes
;
283 obdesc
.bufElSizes
= obuf_el_sizes
;
285 err
= aacEncEncode(encoder
, &ibdesc
, &obdesc
, &iargs
, &oargs
);
286 if (err
!= AACENC_ENCODE_EOF
&& err
!= AACENC_OK
) {
287 fprintf(stderr
, "ERROR: aacEncEncode() failed\n");
290 output
->size
= oargs
.numOutBytes
;
291 return oargs
.numInSamples
/ format
->channels_per_frame
;