Signed-off-by: Martin Storsjö <martin@martin.st>
Martin Storsjö authored on 2012/06/28 22:46:24... | ... |
@@ -170,6 +170,7 @@ External library support: |
170 | 170 |
--enable-libdc1394 enable IIDC-1394 grabbing using libdc1394 |
171 | 171 |
and libraw1394 [no] |
172 | 172 |
--enable-libfaac enable FAAC support via libfaac [no] |
173 |
+ --enable-libfdk-aac enable AAC support via libfdk-aac [no] |
|
173 | 174 |
--enable-libfreetype enable libfreetype [no] |
174 | 175 |
--enable-libgsm enable GSM support via libgsm [no] |
175 | 176 |
--enable-libilbc enable iLBC de/encoding via libilbc [no] |
... | ... |
@@ -943,6 +944,7 @@ CONFIG_LIST=" |
943 | 943 |
libcdio |
944 | 944 |
libdc1394 |
945 | 945 |
libfaac |
946 |
+ libfdk_aac |
|
946 | 947 |
libfreetype |
947 | 948 |
libgsm |
948 | 949 |
libilbc |
... | ... |
@@ -1448,6 +1450,7 @@ h264_parser_select="golomb h264dsp h264pred" |
1448 | 1448 |
|
1449 | 1449 |
# external libraries |
1450 | 1450 |
libfaac_encoder_deps="libfaac" |
1451 |
+libfdk_aac_encoder_deps="libfdk_aac" |
|
1451 | 1452 |
libgsm_decoder_deps="libgsm" |
1452 | 1453 |
libgsm_encoder_deps="libgsm" |
1453 | 1454 |
libgsm_ms_decoder_deps="libgsm" |
... | ... |
@@ -2968,6 +2971,7 @@ enabled avisynth && require2 vfw32 "windows.h vfw.h" AVIFileInit -lavifil32 |
2968 | 2968 |
enabled frei0r && { check_header frei0r.h || die "ERROR: frei0r.h header not found"; } |
2969 | 2969 |
enabled gnutls && require_pkg_config gnutls gnutls/gnutls.h gnutls_global_init |
2970 | 2970 |
enabled libfaac && require2 libfaac "stdint.h faac.h" faacEncGetVersion -lfaac |
2971 |
+enabled libfdk_aac && require libfdk_aac fdk-aac/aacenc_lib.h aacEncOpen -lfdk-aac |
|
2971 | 2972 |
enabled libfreetype && require_pkg_config freetype2 "ft2build.h freetype/freetype.h" FT_Init_FreeType |
2972 | 2973 |
enabled libgsm && require libgsm gsm/gsm.h gsm_create -lgsm |
2973 | 2974 |
enabled libilbc && require libilbc ilbc.h WebRtcIlbcfix_InitDecode -lilbc |
... | ... |
@@ -3259,6 +3263,7 @@ echo "gnutls enabled ${gnutls-no}" |
3259 | 3259 |
echo "libcdio support ${libcdio-no}" |
3260 | 3260 |
echo "libdc1394 support ${libdc1394-no}" |
3261 | 3261 |
echo "libfaac enabled ${libfaac-no}" |
3262 |
+echo "libfdk-aac enabled ${libfdk_aac-no}" |
|
3262 | 3263 |
echo "libgsm enabled ${libgsm-no}" |
3263 | 3264 |
echo "libilbc enabled ${libilbc-no}" |
3264 | 3265 |
echo "libmp3lame enabled ${libmp3lame-no}" |
... | ... |
@@ -18,8 +18,8 @@ explicitly requested by passing the appropriate flags to |
18 | 18 |
|
19 | 19 |
@section OpenCORE and VisualOn libraries |
20 | 20 |
|
21 |
-Spun off Google Android sources, OpenCore and VisualOn libraries provide |
|
22 |
-encoders for a number of audio codecs. |
|
21 |
+Spun off Google Android sources, OpenCore, VisualOn and Fraunhofer |
|
22 |
+libraries provide encoders for a number of audio codecs. |
|
23 | 23 |
|
24 | 24 |
@float NOTE |
25 | 25 |
OpenCORE and VisualOn libraries are under the Apache License 2.0 |
... | ... |
@@ -55,6 +55,14 @@ Go to @url{http://sourceforge.net/projects/opencore-amr/} and follow the |
55 | 55 |
instructions for installing the library. |
56 | 56 |
Then pass @code{--enable-libvo-amrwbenc} to configure to enable it. |
57 | 57 |
|
58 |
+@subsection Fraunhofer AAC library |
|
59 |
+ |
|
60 |
+Libav can make use of the Fraunhofer AAC library for AAC encoding. |
|
61 |
+ |
|
62 |
+Go to @url{http://sourceforge.net/projects/opencore-amr/} and follow the |
|
63 |
+instructions for installing the library. |
|
64 |
+Then pass @code{--enable-libfdk-aac} to configure to enable it. |
|
65 |
+ |
|
58 | 66 |
@section LAME |
59 | 67 |
|
60 | 68 |
Libav can make use of the LAME library for MP3 encoding. |
... | ... |
@@ -595,6 +595,7 @@ OBJS-$(CONFIG_WTV_DEMUXER) += mpeg4audio.o mpegaudiodata.o |
595 | 595 |
|
596 | 596 |
# external codec libraries |
597 | 597 |
OBJS-$(CONFIG_LIBFAAC_ENCODER) += libfaac.o audio_frame_queue.o |
598 |
+OBJS-$(CONFIG_LIBFDK_AAC_ENCODER) += libfdk-aacenc.o audio_frame_queue.o |
|
598 | 599 |
OBJS-$(CONFIG_LIBGSM_DECODER) += libgsm.o |
599 | 600 |
OBJS-$(CONFIG_LIBGSM_ENCODER) += libgsm.o |
600 | 601 |
OBJS-$(CONFIG_LIBGSM_MS_DECODER) += libgsm.o |
... | ... |
@@ -380,6 +380,7 @@ void avcodec_register_all(void) |
380 | 380 |
|
381 | 381 |
/* external libraries */ |
382 | 382 |
REGISTER_ENCODER (LIBFAAC, libfaac); |
383 |
+ REGISTER_ENCODER (LIBFDK_AAC, libfdk_aac); |
|
383 | 384 |
REGISTER_ENCDEC (LIBGSM, libgsm); |
384 | 385 |
REGISTER_ENCDEC (LIBGSM_MS, libgsm_ms); |
385 | 386 |
REGISTER_ENCDEC (LIBILBC, libilbc); |
386 | 387 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,384 @@ |
0 |
+/* |
|
1 |
+ * AAC encoder wrapper |
|
2 |
+ * Copyright (c) 2012 Martin Storsjo |
|
3 |
+ * |
|
4 |
+ * This file is part of Libav. |
|
5 |
+ * |
|
6 |
+ * Libav is free software; you can redistribute it and/or |
|
7 |
+ * modify it under the terms of the GNU Lesser General Public |
|
8 |
+ * License as published by the Free Software Foundation; either |
|
9 |
+ * version 2.1 of the License, or (at your option) any later version. |
|
10 |
+ * |
|
11 |
+ * Libav is distributed in the hope that it will be useful, |
|
12 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 |
+ * Lesser General Public License for more details. |
|
15 |
+ * |
|
16 |
+ * You should have received a copy of the GNU Lesser General Public |
|
17 |
+ * License along with Libav; if not, write to the Free Software |
|
18 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
19 |
+ */ |
|
20 |
+ |
|
21 |
+#include <fdk-aac/aacenc_lib.h> |
|
22 |
+ |
|
23 |
+#include "avcodec.h" |
|
24 |
+#include "audio_frame_queue.h" |
|
25 |
+#include "internal.h" |
|
26 |
+#include "libavutil/audioconvert.h" |
|
27 |
+#include "libavutil/opt.h" |
|
28 |
+ |
|
29 |
+typedef struct AACContext { |
|
30 |
+ const AVClass *class; |
|
31 |
+ HANDLE_AACENCODER handle; |
|
32 |
+ int afterburner; |
|
33 |
+ int eld_sbr; |
|
34 |
+ int signaling; |
|
35 |
+ |
|
36 |
+ AudioFrameQueue afq; |
|
37 |
+} AACContext; |
|
38 |
+ |
|
39 |
+static const AVOption aac_enc_options[] = { |
|
40 |
+ { "afterburner", "Afterburner (improved quality)", offsetof(AACContext, afterburner), AV_OPT_TYPE_INT, { 1 }, 0, 1, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM }, |
|
41 |
+ { "eld_sbr", "Enable SBR for ELD (for SBR in other configurations, use the -profile parameter)", offsetof(AACContext, eld_sbr), AV_OPT_TYPE_INT, { 0 }, 0, 1, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM }, |
|
42 |
+ { "signaling", "SBR/PS signaling style", offsetof(AACContext, signaling), AV_OPT_TYPE_INT, { -1 }, -1, 2, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM, "signaling" }, |
|
43 |
+ { "default", "Choose signaling implicitly (explicit hierarchical by default, implicit if global header is disabled)", 0, AV_OPT_TYPE_CONST, { -1 }, 0, 0, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM, "signaling" }, |
|
44 |
+ { "implicit", "Implicit backwards compatible signaling", 0, AV_OPT_TYPE_CONST, { 0 }, 0, 0, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM, "signaling" }, |
|
45 |
+ { "explicit_sbr", "Explicit SBR, implicit PS signaling", 0, AV_OPT_TYPE_CONST, { 1 }, 0, 0, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM, "signaling" }, |
|
46 |
+ { "explicit_hierarchical", "Explicit hierarchical signaling", 0, AV_OPT_TYPE_CONST, { 2 }, 0, 0, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM, "signaling" }, |
|
47 |
+ { NULL } |
|
48 |
+}; |
|
49 |
+ |
|
50 |
+static const AVClass aac_enc_class = { |
|
51 |
+ "libfdk_aac", av_default_item_name, aac_enc_options, LIBAVUTIL_VERSION_INT |
|
52 |
+}; |
|
53 |
+ |
|
54 |
+static const char *aac_get_error(AACENC_ERROR err) |
|
55 |
+{ |
|
56 |
+ switch (err) { |
|
57 |
+ case AACENC_OK: |
|
58 |
+ return "No error"; |
|
59 |
+ case AACENC_INVALID_HANDLE: |
|
60 |
+ return "Invalid handle"; |
|
61 |
+ case AACENC_MEMORY_ERROR: |
|
62 |
+ return "Memory allocation error"; |
|
63 |
+ case AACENC_UNSUPPORTED_PARAMETER: |
|
64 |
+ return "Unsupported parameter"; |
|
65 |
+ case AACENC_INVALID_CONFIG: |
|
66 |
+ return "Invalid config"; |
|
67 |
+ case AACENC_INIT_ERROR: |
|
68 |
+ return "Initialization error"; |
|
69 |
+ case AACENC_INIT_AAC_ERROR: |
|
70 |
+ return "AAC library initialization error"; |
|
71 |
+ case AACENC_INIT_SBR_ERROR: |
|
72 |
+ return "SBR library initialization error"; |
|
73 |
+ case AACENC_INIT_TP_ERROR: |
|
74 |
+ return "Transport library initialization error"; |
|
75 |
+ case AACENC_INIT_META_ERROR: |
|
76 |
+ return "Metadata library initialization error"; |
|
77 |
+ case AACENC_ENCODE_ERROR: |
|
78 |
+ return "Encoding error"; |
|
79 |
+ case AACENC_ENCODE_EOF: |
|
80 |
+ return "End of file"; |
|
81 |
+ default: |
|
82 |
+ return "Unknown error"; |
|
83 |
+ } |
|
84 |
+} |
|
85 |
+ |
|
86 |
+static int aac_encode_close(AVCodecContext *avctx) |
|
87 |
+{ |
|
88 |
+ AACContext *s = avctx->priv_data; |
|
89 |
+ |
|
90 |
+ if (s->handle) |
|
91 |
+ aacEncClose(&s->handle); |
|
92 |
+#if FF_API_OLD_ENCODE_AUDIO |
|
93 |
+ av_freep(&avctx->coded_frame); |
|
94 |
+#endif |
|
95 |
+ av_freep(&avctx->extradata); |
|
96 |
+ ff_af_queue_close(&s->afq); |
|
97 |
+ |
|
98 |
+ return 0; |
|
99 |
+} |
|
100 |
+ |
|
101 |
+static av_cold int aac_encode_init(AVCodecContext *avctx) |
|
102 |
+{ |
|
103 |
+ AACContext *s = avctx->priv_data; |
|
104 |
+ int ret = AVERROR(EINVAL); |
|
105 |
+ AACENC_InfoStruct info = { 0 }; |
|
106 |
+ CHANNEL_MODE mode; |
|
107 |
+ AACENC_ERROR err; |
|
108 |
+ int aot = FF_PROFILE_AAC_LOW + 1; |
|
109 |
+ int sce = 0, cpe = 0; |
|
110 |
+ |
|
111 |
+ if ((err = aacEncOpen(&s->handle, 0, avctx->channels)) != AACENC_OK) { |
|
112 |
+ av_log(avctx, AV_LOG_ERROR, "Unable to open the encoder: %s\n", |
|
113 |
+ aac_get_error(err)); |
|
114 |
+ goto error; |
|
115 |
+ } |
|
116 |
+ |
|
117 |
+ if (avctx->profile != FF_PROFILE_UNKNOWN) |
|
118 |
+ aot = avctx->profile + 1; |
|
119 |
+ |
|
120 |
+ if ((err = aacEncoder_SetParam(s->handle, AACENC_AOT, aot)) != AACENC_OK) { |
|
121 |
+ av_log(avctx, AV_LOG_ERROR, "Unable to set the AOT %d: %s\n", |
|
122 |
+ aot, aac_get_error(err)); |
|
123 |
+ goto error; |
|
124 |
+ } |
|
125 |
+ |
|
126 |
+ if (aot == FF_PROFILE_AAC_ELD + 1 && s->eld_sbr) { |
|
127 |
+ if ((err = aacEncoder_SetParam(s->handle, AACENC_SBR_MODE, |
|
128 |
+ 1)) != AACENC_OK) { |
|
129 |
+ av_log(avctx, AV_LOG_ERROR, "Unable to enable SBR for ELD: %s\n", |
|
130 |
+ aac_get_error(err)); |
|
131 |
+ goto error; |
|
132 |
+ } |
|
133 |
+ } |
|
134 |
+ |
|
135 |
+ if ((err = aacEncoder_SetParam(s->handle, AACENC_SAMPLERATE, |
|
136 |
+ avctx->sample_rate)) != AACENC_OK) { |
|
137 |
+ av_log(avctx, AV_LOG_ERROR, "Unable to set the sample rate %d: %s\n", |
|
138 |
+ avctx->sample_rate, aac_get_error(err)); |
|
139 |
+ goto error; |
|
140 |
+ } |
|
141 |
+ |
|
142 |
+ switch (avctx->channels) { |
|
143 |
+ case 1: mode = MODE_1; sce = 1; cpe = 0; break; |
|
144 |
+ case 2: mode = MODE_2; sce = 0; cpe = 1; break; |
|
145 |
+ case 3: mode = MODE_1_2; sce = 1; cpe = 1; break; |
|
146 |
+ case 4: mode = MODE_1_2_1; sce = 2; cpe = 1; break; |
|
147 |
+ case 5: mode = MODE_1_2_2; sce = 1; cpe = 2; break; |
|
148 |
+ case 6: mode = MODE_1_2_2_1; sce = 2; cpe = 2; break; |
|
149 |
+ default: |
|
150 |
+ av_log(avctx, AV_LOG_ERROR, |
|
151 |
+ "Unsupported number of channels %d\n", avctx->channels); |
|
152 |
+ goto error; |
|
153 |
+ } |
|
154 |
+ |
|
155 |
+ if ((err = aacEncoder_SetParam(s->handle, AACENC_CHANNELMODE, |
|
156 |
+ mode)) != AACENC_OK) { |
|
157 |
+ av_log(avctx, AV_LOG_ERROR, |
|
158 |
+ "Unable to set channel mode %d: %s\n", mode, aac_get_error(err)); |
|
159 |
+ goto error; |
|
160 |
+ } |
|
161 |
+ |
|
162 |
+ if ((err = aacEncoder_SetParam(s->handle, AACENC_CHANNELORDER, |
|
163 |
+ 1)) != AACENC_OK) { |
|
164 |
+ av_log(avctx, AV_LOG_ERROR, |
|
165 |
+ "Unable to set wav channel order %d: %s\n", |
|
166 |
+ mode, aac_get_error(err)); |
|
167 |
+ goto error; |
|
168 |
+ } |
|
169 |
+ |
|
170 |
+ if (avctx->flags & CODEC_FLAG_QSCALE) { |
|
171 |
+ int mode = avctx->global_quality; |
|
172 |
+ if (mode < 1 || mode > 5) { |
|
173 |
+ av_log(avctx, AV_LOG_WARNING, |
|
174 |
+ "VBR quality %d out of range, should be 1-5\n", mode); |
|
175 |
+ mode = av_clip(mode, 1, 5); |
|
176 |
+ } |
|
177 |
+ if ((err = aacEncoder_SetParam(s->handle, AACENC_BITRATEMODE, |
|
178 |
+ mode)) != AACENC_OK) { |
|
179 |
+ av_log(avctx, AV_LOG_ERROR, "Unable to set the VBR bitrate mode %d: %s\n", |
|
180 |
+ mode, aac_get_error(err)); |
|
181 |
+ goto error; |
|
182 |
+ } |
|
183 |
+ } else { |
|
184 |
+ if (avctx->bit_rate <= 0) { |
|
185 |
+ if (avctx->profile == FF_PROFILE_AAC_HE_V2) { |
|
186 |
+ sce = 1; |
|
187 |
+ cpe = 0; |
|
188 |
+ } |
|
189 |
+ avctx->bit_rate = (96*sce + 128*cpe) * avctx->sample_rate / 44; |
|
190 |
+ if (avctx->profile == FF_PROFILE_AAC_HE || |
|
191 |
+ avctx->profile == FF_PROFILE_AAC_HE_V2 || |
|
192 |
+ s->eld_sbr) |
|
193 |
+ avctx->bit_rate /= 2; |
|
194 |
+ } |
|
195 |
+ if ((err = aacEncoder_SetParam(s->handle, AACENC_BITRATE, |
|
196 |
+ avctx->bit_rate)) != AACENC_OK) { |
|
197 |
+ av_log(avctx, AV_LOG_ERROR, "Unable to set the bitrate %d: %s\n", |
|
198 |
+ avctx->bit_rate, aac_get_error(err)); |
|
199 |
+ goto error; |
|
200 |
+ } |
|
201 |
+ } |
|
202 |
+ |
|
203 |
+ /* Choose bitstream format - if global header is requested, use |
|
204 |
+ * raw access units, otherwise use ADTS. */ |
|
205 |
+ if ((err = aacEncoder_SetParam(s->handle, AACENC_TRANSMUX, |
|
206 |
+ avctx->flags & CODEC_FLAG_GLOBAL_HEADER ? 0 : 2)) != AACENC_OK) { |
|
207 |
+ av_log(avctx, AV_LOG_ERROR, "Unable to set the transmux format: %s\n", |
|
208 |
+ aac_get_error(err)); |
|
209 |
+ goto error; |
|
210 |
+ } |
|
211 |
+ |
|
212 |
+ /* If no signaling mode is chosen, use explicit hierarchical signaling |
|
213 |
+ * if using mp4 mode (raw access units, with global header) and |
|
214 |
+ * implicit signaling if using ADTS. */ |
|
215 |
+ if (s->signaling < 0) |
|
216 |
+ s->signaling = avctx->flags & CODEC_FLAG_GLOBAL_HEADER ? 2 : 0; |
|
217 |
+ |
|
218 |
+ if ((err = aacEncoder_SetParam(s->handle, AACENC_SIGNALING_MODE, |
|
219 |
+ s->signaling)) != AACENC_OK) { |
|
220 |
+ av_log(avctx, AV_LOG_ERROR, "Unable to set signaling mode %d: %s\n", |
|
221 |
+ s->signaling, aac_get_error(err)); |
|
222 |
+ goto error; |
|
223 |
+ } |
|
224 |
+ |
|
225 |
+ if ((err = aacEncoder_SetParam(s->handle, AACENC_AFTERBURNER, |
|
226 |
+ s->afterburner)) != AACENC_OK) { |
|
227 |
+ av_log(avctx, AV_LOG_ERROR, "Unable to set afterburner to %d: %s\n", |
|
228 |
+ s->afterburner, aac_get_error(err)); |
|
229 |
+ goto error; |
|
230 |
+ } |
|
231 |
+ |
|
232 |
+ if ((err = aacEncEncode(s->handle, NULL, NULL, NULL, NULL)) != AACENC_OK) { |
|
233 |
+ av_log(avctx, AV_LOG_ERROR, "Unable to initialize the encoder: %s\n", |
|
234 |
+ aac_get_error(err)); |
|
235 |
+ return AVERROR(EINVAL); |
|
236 |
+ } |
|
237 |
+ |
|
238 |
+ if ((err = aacEncInfo(s->handle, &info)) != AACENC_OK) { |
|
239 |
+ av_log(avctx, AV_LOG_ERROR, "Unable to get encoder info: %s\n", |
|
240 |
+ aac_get_error(err)); |
|
241 |
+ goto error; |
|
242 |
+ } |
|
243 |
+ |
|
244 |
+#if FF_API_OLD_ENCODE_AUDIO |
|
245 |
+ avctx->coded_frame = avcodec_alloc_frame(); |
|
246 |
+ if (!avctx->coded_frame) { |
|
247 |
+ ret = AVERROR(ENOMEM); |
|
248 |
+ goto error; |
|
249 |
+ } |
|
250 |
+#endif |
|
251 |
+ avctx->frame_size = info.frameLength; |
|
252 |
+ avctx->delay = info.encoderDelay; |
|
253 |
+ ff_af_queue_init(avctx, &s->afq); |
|
254 |
+ |
|
255 |
+ if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) { |
|
256 |
+ avctx->extradata_size = info.confSize; |
|
257 |
+ avctx->extradata = av_mallocz(avctx->extradata_size + |
|
258 |
+ FF_INPUT_BUFFER_PADDING_SIZE); |
|
259 |
+ if (!avctx->extradata) { |
|
260 |
+ ret = AVERROR(ENOMEM); |
|
261 |
+ goto error; |
|
262 |
+ } |
|
263 |
+ |
|
264 |
+ memcpy(avctx->extradata, info.confBuf, info.confSize); |
|
265 |
+ } |
|
266 |
+ return 0; |
|
267 |
+error: |
|
268 |
+ aac_encode_close(avctx); |
|
269 |
+ return ret; |
|
270 |
+} |
|
271 |
+ |
|
272 |
+static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, |
|
273 |
+ const AVFrame *frame, int *got_packet_ptr) |
|
274 |
+{ |
|
275 |
+ AACContext *s = avctx->priv_data; |
|
276 |
+ AACENC_BufDesc in_buf = { 0 }, out_buf = { 0 }; |
|
277 |
+ AACENC_InArgs in_args = { 0 }; |
|
278 |
+ AACENC_OutArgs out_args = { 0 }; |
|
279 |
+ int in_buffer_identifier = IN_AUDIO_DATA; |
|
280 |
+ int in_buffer_size, in_buffer_element_size; |
|
281 |
+ int out_buffer_identifier = OUT_BITSTREAM_DATA; |
|
282 |
+ int out_buffer_size, out_buffer_element_size; |
|
283 |
+ void *in_ptr, *out_ptr; |
|
284 |
+ int ret; |
|
285 |
+ AACENC_ERROR err; |
|
286 |
+ |
|
287 |
+ /* handle end-of-stream small frame and flushing */ |
|
288 |
+ if (!frame) { |
|
289 |
+ in_args.numInSamples = -1; |
|
290 |
+ } else { |
|
291 |
+ in_ptr = frame->data[0]; |
|
292 |
+ in_buffer_size = 2 * avctx->channels * frame->nb_samples; |
|
293 |
+ in_buffer_element_size = 2; |
|
294 |
+ |
|
295 |
+ in_args.numInSamples = avctx->channels * frame->nb_samples; |
|
296 |
+ in_buf.numBufs = 1; |
|
297 |
+ in_buf.bufs = &in_ptr; |
|
298 |
+ in_buf.bufferIdentifiers = &in_buffer_identifier; |
|
299 |
+ in_buf.bufSizes = &in_buffer_size; |
|
300 |
+ in_buf.bufElSizes = &in_buffer_element_size; |
|
301 |
+ |
|
302 |
+ /* add current frame to the queue */ |
|
303 |
+ if ((ret = ff_af_queue_add(&s->afq, frame) < 0)) |
|
304 |
+ return ret; |
|
305 |
+ } |
|
306 |
+ |
|
307 |
+ /* The maximum packet size is 6144 bits aka 768 bytes per channel. */ |
|
308 |
+ if ((ret = ff_alloc_packet(avpkt, FFMAX(8192, 768 * avctx->channels)))) { |
|
309 |
+ av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n"); |
|
310 |
+ return ret; |
|
311 |
+ } |
|
312 |
+ |
|
313 |
+ out_ptr = avpkt->data; |
|
314 |
+ out_buffer_size = avpkt->size; |
|
315 |
+ out_buffer_element_size = 1; |
|
316 |
+ out_buf.numBufs = 1; |
|
317 |
+ out_buf.bufs = &out_ptr; |
|
318 |
+ out_buf.bufferIdentifiers = &out_buffer_identifier; |
|
319 |
+ out_buf.bufSizes = &out_buffer_size; |
|
320 |
+ out_buf.bufElSizes = &out_buffer_element_size; |
|
321 |
+ |
|
322 |
+ if ((err = aacEncEncode(s->handle, &in_buf, &out_buf, &in_args, |
|
323 |
+ &out_args)) != AACENC_OK) { |
|
324 |
+ if (!frame && err == AACENC_ENCODE_EOF) |
|
325 |
+ return 0; |
|
326 |
+ av_log(avctx, AV_LOG_ERROR, "Unable to encode frame: %s\n", |
|
327 |
+ aac_get_error(err)); |
|
328 |
+ return AVERROR(EINVAL); |
|
329 |
+ } |
|
330 |
+ |
|
331 |
+ if (!out_args.numOutBytes) |
|
332 |
+ return 0; |
|
333 |
+ |
|
334 |
+ /* Get the next frame pts & duration */ |
|
335 |
+ ff_af_queue_remove(&s->afq, avctx->frame_size, &avpkt->pts, |
|
336 |
+ &avpkt->duration); |
|
337 |
+ |
|
338 |
+ avpkt->size = out_args.numOutBytes; |
|
339 |
+ *got_packet_ptr = 1; |
|
340 |
+ return 0; |
|
341 |
+} |
|
342 |
+ |
|
343 |
+static const AVProfile profiles[] = { |
|
344 |
+ { FF_PROFILE_AAC_LOW, "LC" }, |
|
345 |
+ { FF_PROFILE_AAC_HE, "HE-AAC" }, |
|
346 |
+ { FF_PROFILE_AAC_HE_V2, "HE-AACv2" }, |
|
347 |
+ { FF_PROFILE_AAC_LD, "LD" }, |
|
348 |
+ { FF_PROFILE_AAC_ELD, "ELD" }, |
|
349 |
+ { FF_PROFILE_UNKNOWN }, |
|
350 |
+}; |
|
351 |
+ |
|
352 |
+static const AVCodecDefault aac_encode_defaults[] = { |
|
353 |
+ { "b", "0" }, |
|
354 |
+ { NULL } |
|
355 |
+}; |
|
356 |
+ |
|
357 |
+static const uint64_t aac_channel_layout[] = { |
|
358 |
+ AV_CH_LAYOUT_MONO, |
|
359 |
+ AV_CH_LAYOUT_STEREO, |
|
360 |
+ AV_CH_LAYOUT_SURROUND, |
|
361 |
+ AV_CH_LAYOUT_4POINT0, |
|
362 |
+ AV_CH_LAYOUT_5POINT0_BACK, |
|
363 |
+ AV_CH_LAYOUT_5POINT1_BACK, |
|
364 |
+ 0, |
|
365 |
+}; |
|
366 |
+ |
|
367 |
+AVCodec ff_libfdk_aac_encoder = { |
|
368 |
+ .name = "libfdk_aac", |
|
369 |
+ .type = AVMEDIA_TYPE_AUDIO, |
|
370 |
+ .id = CODEC_ID_AAC, |
|
371 |
+ .priv_data_size = sizeof(AACContext), |
|
372 |
+ .init = aac_encode_init, |
|
373 |
+ .encode2 = aac_encode_frame, |
|
374 |
+ .close = aac_encode_close, |
|
375 |
+ .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY, |
|
376 |
+ .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16, |
|
377 |
+ AV_SAMPLE_FMT_NONE }, |
|
378 |
+ .long_name = NULL_IF_CONFIG_SMALL("Fraunhofer FDK AAC"), |
|
379 |
+ .priv_class = &aac_enc_class, |
|
380 |
+ .defaults = aac_encode_defaults, |
|
381 |
+ .profiles = profiles, |
|
382 |
+ .channel_layouts = aac_channel_layout, |
|
383 |
+}; |