]>
Commit | Line | Data |
---|---|---|
48e2f01c | 1 | /* |
2 | * Copyright (C) 2013 nu774 | |
3 | * For conditions of distribution and use, see copyright notice in COPYING | |
4 | */ | |
5 | #if HAVE_CONFIG_H | |
6 | # include "config.h" | |
7 | #endif | |
8 | #if HAVE_STDINT_H | |
9 | # include <stdint.h> | |
10 | #endif | |
11 | #include <stdio.h> | |
12 | #include <stdlib.h> | |
9b8f9915 | 13 | #include <string.h> |
48e2f01c | 14 | #include "aacenc.h" |
15 | ||
16 | int aacenc_is_sbr_active(const aacenc_param_t *params) | |
17 | { | |
18 | switch (params->profile) { | |
19 | case AOT_SBR: case AOT_PS: case AOT_MP2_SBR: case AOT_MP2_PS: | |
20 | case AOT_DABPLUS_SBR: case AOT_DABPLUS_PS: | |
21 | case AOT_DRM_SBR: case AOT_DRM_MPEG_PS: | |
22 | return 1; | |
23 | } | |
24 | if (params->profile == AOT_ER_AAC_ELD && params->lowdelay_sbr) | |
25 | return 1; | |
26 | return 0; | |
27 | } | |
28 | ||
9b8f9915 | 29 | static const unsigned aacenc_sampling_freq_tab[] = { |
30 | 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, | |
31 | 16000, 12000, 11025, 8000, 7350, 0, 0, 0 | |
32 | }; | |
33 | ||
34 | static | |
35 | unsigned sampling_freq_index(unsigned rate) | |
36 | { | |
37 | unsigned i; | |
38 | for (i = 0; aacenc_sampling_freq_tab[i]; ++i) | |
39 | if (aacenc_sampling_freq_tab[i] == rate) | |
40 | return i; | |
41 | return 0xf; | |
42 | } | |
43 | ||
44 | /* | |
45 | * Append backward compatible SBR/PS signaling to implicit signaling ASC, | |
46 | * if SBR/PS is present. | |
47 | */ | |
48 | int aacenc_mp4asc(const aacenc_param_t *params, | |
49 | const uint8_t *asc, uint32_t ascsize, | |
50 | uint8_t *outasc, uint32_t *outsize) | |
51 | { | |
52 | unsigned asc_sfreq = aacenc_sampling_freq_tab[(asc[0]&0x7)<<1 |asc[1]>>7]; | |
53 | ||
54 | switch (params->profile) { | |
55 | case AOT_SBR: | |
56 | case AOT_PS: | |
57 | if (*outsize < ascsize + 3) | |
58 | return -1; | |
59 | memcpy(outasc, asc, ascsize); | |
60 | /* syncExtensionType:11 (value:0x2b7) */ | |
61 | outasc[ascsize+0] = 0x2b << 1; | |
62 | outasc[ascsize+1] = 0x7 << 5; | |
63 | /* extensionAudioObjectType:5 (value:5)*/ | |
64 | outasc[ascsize+1] |= 5; | |
65 | /* sbrPresentFlag:1 (value:1) */ | |
66 | outasc[ascsize+2] = 0x80; | |
67 | /* extensionSamplingFrequencyIndex:4 */ | |
68 | outasc[ascsize+2] |= sampling_freq_index(asc_sfreq << 1) << 3; | |
69 | if (params->profile == AOT_SBR) { | |
70 | *outsize = ascsize + 3; | |
71 | break; | |
72 | } | |
73 | if (*outsize < ascsize + 5) | |
74 | return -1; | |
75 | /* syncExtensionType:11 (value:0x548) */ | |
76 | outasc[ascsize+2] |= 0x5; | |
77 | outasc[ascsize+3] = 0x48; | |
78 | /* psPresentFlag:1 (value:1) */ | |
79 | outasc[ascsize+4] = 0x80; | |
80 | *outsize = ascsize + 5; | |
81 | break; | |
82 | default: | |
83 | if (*outsize < ascsize) | |
84 | return -1; | |
85 | memcpy(outasc, asc, ascsize); | |
86 | *outsize = ascsize; | |
87 | } | |
88 | return 0; | |
89 | } | |
90 | ||
48e2f01c | 91 | static |
92 | int aacenc_channel_mode(const pcm_sample_description_t *format) | |
93 | { | |
94 | uint32_t chanmask = format->channel_mask; | |
95 | ||
96 | if (format->channels_per_frame > 6) | |
97 | return 0; | |
98 | if (!chanmask) { | |
99 | static uint32_t defaults[] = { 0x4, 0x3, 0x7, 0, 0x37, 0x3f }; | |
100 | chanmask = defaults[format->channels_per_frame - 1]; | |
101 | } | |
102 | switch (chanmask) { | |
103 | case 0x3: return MODE_2; | |
104 | case 0x4: return MODE_1; | |
105 | case 0x7: return MODE_1_2; | |
106 | case 0x37: return MODE_1_2_2; | |
107 | case 0x3f: return MODE_1_2_2_1; | |
108 | case 0x107: return MODE_1_2_1; | |
109 | case 0x607: return MODE_1_2_2; | |
110 | case 0x60f: return MODE_1_2_2_1; | |
111 | } | |
112 | return 0; | |
113 | } | |
114 | ||
115 | int aacenc_init(HANDLE_AACENCODER *encoder, const aacenc_param_t *params, | |
116 | const pcm_sample_description_t *format, | |
117 | AACENC_InfoStruct *info) | |
118 | { | |
119 | int channel_mode; | |
120 | int aot; | |
121 | ||
122 | *encoder = 0; | |
123 | if ((channel_mode = aacenc_channel_mode(format)) == 0) { | |
124 | fprintf(stderr, "ERROR: unsupported channel layout\n"); | |
125 | goto FAIL; | |
126 | } | |
127 | if (aacEncOpen(encoder, 0, 0) != AACENC_OK) { | |
128 | fprintf(stderr, "ERROR: aacEncOpen() failed\n"); | |
129 | goto FAIL; | |
130 | } | |
131 | aot = (params->profile ? params->profile : AOT_AAC_LC); | |
132 | if (aacEncoder_SetParam(*encoder, AACENC_AOT, aot) != AACENC_OK) { | |
133 | fprintf(stderr, "ERROR: unsupported profile\n"); | |
134 | goto FAIL; | |
135 | } | |
136 | if (params->bitrate_mode == 0) | |
137 | aacEncoder_SetParam(*encoder, AACENC_BITRATE, params->bitrate); | |
138 | else if (aacEncoder_SetParam(*encoder, AACENC_BITRATEMODE, | |
139 | params->bitrate_mode) != AACENC_OK) { | |
140 | fprintf(stderr, "ERROR: unsupported bitrate mode\n"); | |
141 | goto FAIL; | |
142 | } | |
143 | if (aacEncoder_SetParam(*encoder, AACENC_SAMPLERATE, | |
144 | format->sample_rate) != AACENC_OK) { | |
145 | fprintf(stderr, "ERROR: unsupported sample rate\n"); | |
146 | goto FAIL; | |
147 | } | |
148 | aacEncoder_SetParam(*encoder, AACENC_CHANNELMODE, channel_mode); | |
149 | aacEncoder_SetParam(*encoder, AACENC_BANDWIDTH, params->bandwidth); | |
150 | aacEncoder_SetParam(*encoder, AACENC_CHANNELORDER, 1); | |
151 | aacEncoder_SetParam(*encoder, AACENC_AFTERBURNER, !!params->afterburner); | |
152 | ||
153 | if (aot == AOT_ER_AAC_ELD && params->lowdelay_sbr) | |
154 | aacEncoder_SetParam(*encoder, AACENC_SBR_MODE, 1); | |
155 | ||
156 | if (aacEncoder_SetParam(*encoder, AACENC_TRANSMUX, | |
157 | params->transport_format) != AACENC_OK) { | |
158 | fprintf(stderr, "ERROR: unsupported transport format\n"); | |
159 | goto FAIL; | |
160 | } | |
161 | if (aacEncoder_SetParam(*encoder, AACENC_SIGNALING_MODE, | |
162 | params->sbr_signaling) != AACENC_OK) { | |
f5363b23 | 163 | fprintf(stderr, "ERROR: failed to set SBR signaling mode\n"); |
48e2f01c | 164 | goto FAIL; |
165 | } | |
166 | if (params->adts_crc_check) | |
167 | aacEncoder_SetParam(*encoder, AACENC_PROTECTION, 1); | |
168 | if (params->header_period) | |
169 | aacEncoder_SetParam(*encoder, AACENC_HEADER_PERIOD, | |
170 | params->header_period); | |
171 | ||
172 | if (aacEncEncode(*encoder, 0, 0, 0, 0) != AACENC_OK) { | |
173 | fprintf(stderr, "ERROR: encoder initialization failed\n"); | |
174 | goto FAIL; | |
175 | } | |
176 | if (aacEncInfo(*encoder, info) != AACENC_OK) { | |
177 | fprintf(stderr, "ERROR: cannot retrieve encoder info\n"); | |
178 | goto FAIL; | |
179 | } | |
180 | return 0; | |
181 | FAIL: | |
182 | if (encoder) | |
183 | aacEncClose(encoder); | |
184 | return -1; | |
185 | } | |
186 | ||
187 | int aac_encode_frame(HANDLE_AACENCODER encoder, | |
188 | const pcm_sample_description_t *format, | |
189 | const int16_t *input, unsigned iframes, | |
190 | uint8_t **output, uint32_t *olen, uint32_t *osize) | |
191 | { | |
192 | uint32_t ilen = iframes * format->channels_per_frame; | |
193 | AACENC_BufDesc ibdesc = { 0 }, obdesc = { 0 }; | |
194 | AACENC_InArgs iargs = { 0 }; | |
195 | AACENC_OutArgs oargs = { 0 }; | |
196 | void *ibufs[] = { (void*)input }; | |
197 | void *obufs[1]; | |
198 | INT ibuf_ids[] = { IN_AUDIO_DATA }; | |
199 | INT obuf_ids[] = { OUT_BITSTREAM_DATA }; | |
200 | INT ibuf_sizes[] = { ilen * sizeof(int16_t) }; | |
201 | INT obuf_sizes[1]; | |
202 | INT ibuf_el_sizes[] = { sizeof(int16_t) }; | |
203 | INT obuf_el_sizes[] = { 1 }; | |
204 | AACENC_ERROR err; | |
205 | unsigned channel_mode, obytes; | |
206 | ||
207 | channel_mode = aacEncoder_GetParam(encoder, AACENC_CHANNELMODE); | |
d52b8bbf | 208 | obytes = 6144 / 8 * channel_mode + 7; |
48e2f01c | 209 | if (!*output || *osize < obytes) { |
210 | *osize = obytes; | |
211 | *output = realloc(*output, obytes); | |
212 | } | |
213 | obufs[0] = *output; | |
214 | obuf_sizes[0] = obytes; | |
215 | ||
216 | iargs.numInSamples = ilen ? ilen : -1; /* -1 for signaling EOF */ | |
217 | ibdesc.numBufs = 1; | |
218 | ibdesc.bufs = ibufs; | |
219 | ibdesc.bufferIdentifiers = ibuf_ids; | |
220 | ibdesc.bufSizes = ibuf_sizes; | |
221 | ibdesc.bufElSizes = ibuf_el_sizes; | |
222 | obdesc.numBufs = 1; | |
223 | obdesc.bufs = obufs; | |
224 | obdesc.bufferIdentifiers = obuf_ids; | |
225 | obdesc.bufSizes = obuf_sizes; | |
226 | obdesc.bufElSizes = obuf_el_sizes; | |
227 | ||
228 | err = aacEncEncode(encoder, &ibdesc, &obdesc, &iargs, &oargs); | |
229 | if (err != AACENC_ENCODE_EOF && err != AACENC_OK) { | |
230 | fprintf(stderr, "ERROR: aacEncEncode() failed\n"); | |
231 | return -1; | |
232 | } | |
233 | *olen = oargs.numOutBytes; | |
234 | return oargs.numInSamples; | |
235 | } |