2 * Copyright (C) 2013 nu774
3 * For conditions of distribution and use, see copyright notice in COPYING
15 int aacenc_is_sbr_active(const aacenc_param_t
*params
)
17 switch (params
->profile
) {
18 case AOT_SBR
: case AOT_PS
: case AOT_MP2_SBR
: case AOT_MP2_PS
:
19 case AOT_DABPLUS_SBR
: case AOT_DABPLUS_PS
:
20 case AOT_DRM_SBR
: case AOT_DRM_MPEG_PS
:
23 if (params
->profile
== AOT_ER_AAC_ELD
&& params
->lowdelay_sbr
)
29 int aacenc_channel_mode(const pcm_sample_description_t
*format
)
31 uint32_t chanmask
= format
->channel_mask
;
33 if (format
->channels_per_frame
> 6)
36 static uint32_t defaults
[] = { 0x4, 0x3, 0x7, 0, 0x37, 0x3f };
37 chanmask
= defaults
[format
->channels_per_frame
- 1];
40 case 0x3: return MODE_2
;
41 case 0x4: return MODE_1
;
42 case 0x7: return MODE_1_2
;
43 case 0x37: return MODE_1_2_2
;
44 case 0x3f: return MODE_1_2_2_1
;
45 case 0x107: return MODE_1_2_1
;
46 case 0x607: return MODE_1_2_2
;
47 case 0x60f: return MODE_1_2_2_1
;
52 int aacenc_init(HANDLE_AACENCODER
*encoder
, const aacenc_param_t
*params
,
53 const pcm_sample_description_t
*format
,
54 AACENC_InfoStruct
*info
)
60 if ((channel_mode
= aacenc_channel_mode(format
)) == 0) {
61 fprintf(stderr
, "ERROR: unsupported channel layout\n");
64 if (aacEncOpen(encoder
, 0, 0) != AACENC_OK
) {
65 fprintf(stderr
, "ERROR: aacEncOpen() failed\n");
68 aot
= (params
->profile
? params
->profile
: AOT_AAC_LC
);
69 if (aacEncoder_SetParam(*encoder
, AACENC_AOT
, aot
) != AACENC_OK
) {
70 fprintf(stderr
, "ERROR: unsupported profile\n");
73 if (params
->bitrate_mode
== 0)
74 aacEncoder_SetParam(*encoder
, AACENC_BITRATE
, params
->bitrate
);
75 else if (aacEncoder_SetParam(*encoder
, AACENC_BITRATEMODE
,
76 params
->bitrate_mode
) != AACENC_OK
) {
77 fprintf(stderr
, "ERROR: unsupported bitrate mode\n");
80 if (aacEncoder_SetParam(*encoder
, AACENC_SAMPLERATE
,
81 format
->sample_rate
) != AACENC_OK
) {
82 fprintf(stderr
, "ERROR: unsupported sample rate\n");
85 aacEncoder_SetParam(*encoder
, AACENC_CHANNELMODE
, channel_mode
);
86 aacEncoder_SetParam(*encoder
, AACENC_BANDWIDTH
, params
->bandwidth
);
87 aacEncoder_SetParam(*encoder
, AACENC_CHANNELORDER
, 1);
88 aacEncoder_SetParam(*encoder
, AACENC_AFTERBURNER
, !!params
->afterburner
);
90 if (aot
== AOT_ER_AAC_ELD
&& params
->lowdelay_sbr
)
91 aacEncoder_SetParam(*encoder
, AACENC_SBR_MODE
, 1);
93 if (aacEncoder_SetParam(*encoder
, AACENC_TRANSMUX
,
94 params
->transport_format
) != AACENC_OK
) {
95 fprintf(stderr
, "ERROR: unsupported transport format\n");
98 if (aacEncoder_SetParam(*encoder
, AACENC_SIGNALING_MODE
,
99 params
->sbr_signaling
) != AACENC_OK
) {
100 fprintf(stderr
, "ERROR: unsupported transport format\n");
103 if (params
->adts_crc_check
)
104 aacEncoder_SetParam(*encoder
, AACENC_PROTECTION
, 1);
105 if (params
->header_period
)
106 aacEncoder_SetParam(*encoder
, AACENC_HEADER_PERIOD
,
107 params
->header_period
);
109 if (aacEncEncode(*encoder
, 0, 0, 0, 0) != AACENC_OK
) {
110 fprintf(stderr
, "ERROR: encoder initialization failed\n");
113 if (aacEncInfo(*encoder
, info
) != AACENC_OK
) {
114 fprintf(stderr
, "ERROR: cannot retrieve encoder info\n");
120 aacEncClose(encoder
);
124 int aac_encode_frame(HANDLE_AACENCODER encoder
,
125 const pcm_sample_description_t
*format
,
126 const int16_t *input
, unsigned iframes
,
127 uint8_t **output
, uint32_t *olen
, uint32_t *osize
)
129 uint32_t ilen
= iframes
* format
->channels_per_frame
;
130 AACENC_BufDesc ibdesc
= { 0 }, obdesc
= { 0 };
131 AACENC_InArgs iargs
= { 0 };
132 AACENC_OutArgs oargs
= { 0 };
133 void *ibufs
[] = { (void*)input
};
135 INT ibuf_ids
[] = { IN_AUDIO_DATA
};
136 INT obuf_ids
[] = { OUT_BITSTREAM_DATA
};
137 INT ibuf_sizes
[] = { ilen
* sizeof(int16_t) };
139 INT ibuf_el_sizes
[] = { sizeof(int16_t) };
140 INT obuf_el_sizes
[] = { 1 };
142 unsigned channel_mode
, obytes
;
144 channel_mode
= aacEncoder_GetParam(encoder
, AACENC_CHANNELMODE
);
145 obytes
= 6144 / 8 * channel_mode
+ 7;
146 if (!*output
|| *osize
< obytes
) {
148 *output
= realloc(*output
, obytes
);
151 obuf_sizes
[0] = obytes
;
153 iargs
.numInSamples
= ilen
? ilen
: -1; /* -1 for signaling EOF */
156 ibdesc
.bufferIdentifiers
= ibuf_ids
;
157 ibdesc
.bufSizes
= ibuf_sizes
;
158 ibdesc
.bufElSizes
= ibuf_el_sizes
;
161 obdesc
.bufferIdentifiers
= obuf_ids
;
162 obdesc
.bufSizes
= obuf_sizes
;
163 obdesc
.bufElSizes
= obuf_el_sizes
;
165 err
= aacEncEncode(encoder
, &ibdesc
, &obdesc
, &iargs
, &oargs
);
166 if (err
!= AACENC_ENCODE_EOF
&& err
!= AACENC_OK
) {
167 fprintf(stderr
, "ERROR: aacEncEncode() failed\n");
170 *olen
= oargs
.numOutBytes
;
171 return oargs
.numInSamples
;