]>
Commit | Line | Data |
---|---|---|
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> | |
13 | #include "aacenc.h" | |
14 | ||
15 | int aacenc_is_sbr_active(const aacenc_param_t *params) | |
16 | { | |
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: | |
21 | return 1; | |
22 | } | |
23 | if (params->profile == AOT_ER_AAC_ELD && params->lowdelay_sbr) | |
24 | return 1; | |
25 | return 0; | |
26 | } | |
27 | ||
28 | static | |
29 | int aacenc_channel_mode(const pcm_sample_description_t *format) | |
30 | { | |
31 | uint32_t chanmask = format->channel_mask; | |
32 | ||
33 | if (format->channels_per_frame > 6) | |
34 | return 0; | |
35 | if (!chanmask) { | |
36 | static uint32_t defaults[] = { 0x4, 0x3, 0x7, 0, 0x37, 0x3f }; | |
37 | chanmask = defaults[format->channels_per_frame - 1]; | |
38 | } | |
39 | switch (chanmask) { | |
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; | |
48 | } | |
49 | return 0; | |
50 | } | |
51 | ||
52 | int aacenc_init(HANDLE_AACENCODER *encoder, const aacenc_param_t *params, | |
53 | const pcm_sample_description_t *format, | |
54 | AACENC_InfoStruct *info) | |
55 | { | |
56 | int channel_mode; | |
57 | int aot; | |
58 | ||
59 | *encoder = 0; | |
60 | if ((channel_mode = aacenc_channel_mode(format)) == 0) { | |
61 | fprintf(stderr, "ERROR: unsupported channel layout\n"); | |
62 | goto FAIL; | |
63 | } | |
64 | if (aacEncOpen(encoder, 0, 0) != AACENC_OK) { | |
65 | fprintf(stderr, "ERROR: aacEncOpen() failed\n"); | |
66 | goto FAIL; | |
67 | } | |
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"); | |
71 | goto FAIL; | |
72 | } | |
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"); | |
78 | goto FAIL; | |
79 | } | |
80 | if (aacEncoder_SetParam(*encoder, AACENC_SAMPLERATE, | |
81 | format->sample_rate) != AACENC_OK) { | |
82 | fprintf(stderr, "ERROR: unsupported sample rate\n"); | |
83 | goto FAIL; | |
84 | } | |
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); | |
89 | ||
90 | if (aot == AOT_ER_AAC_ELD && params->lowdelay_sbr) | |
91 | aacEncoder_SetParam(*encoder, AACENC_SBR_MODE, 1); | |
92 | ||
93 | if (aacEncoder_SetParam(*encoder, AACENC_TRANSMUX, | |
94 | params->transport_format) != AACENC_OK) { | |
95 | fprintf(stderr, "ERROR: unsupported transport format\n"); | |
96 | goto FAIL; | |
97 | } | |
98 | if (aacEncoder_SetParam(*encoder, AACENC_SIGNALING_MODE, | |
99 | params->sbr_signaling) != AACENC_OK) { | |
100 | fprintf(stderr, "ERROR: unsupported transport format\n"); | |
101 | goto FAIL; | |
102 | } | |
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); | |
108 | ||
109 | if (aacEncEncode(*encoder, 0, 0, 0, 0) != AACENC_OK) { | |
110 | fprintf(stderr, "ERROR: encoder initialization failed\n"); | |
111 | goto FAIL; | |
112 | } | |
113 | if (aacEncInfo(*encoder, info) != AACENC_OK) { | |
114 | fprintf(stderr, "ERROR: cannot retrieve encoder info\n"); | |
115 | goto FAIL; | |
116 | } | |
117 | return 0; | |
118 | FAIL: | |
119 | if (encoder) | |
120 | aacEncClose(encoder); | |
121 | return -1; | |
122 | } | |
123 | ||
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) | |
128 | { | |
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 }; | |
134 | void *obufs[1]; | |
135 | INT ibuf_ids[] = { IN_AUDIO_DATA }; | |
136 | INT obuf_ids[] = { OUT_BITSTREAM_DATA }; | |
137 | INT ibuf_sizes[] = { ilen * sizeof(int16_t) }; | |
138 | INT obuf_sizes[1]; | |
139 | INT ibuf_el_sizes[] = { sizeof(int16_t) }; | |
140 | INT obuf_el_sizes[] = { 1 }; | |
141 | AACENC_ERROR err; | |
142 | unsigned channel_mode, obytes; | |
143 | ||
144 | channel_mode = aacEncoder_GetParam(encoder, AACENC_CHANNELMODE); | |
145 | obytes = 6144 / 8 * channel_mode + 7; | |
146 | if (!*output || *osize < obytes) { | |
147 | *osize = obytes; | |
148 | *output = realloc(*output, obytes); | |
149 | } | |
150 | obufs[0] = *output; | |
151 | obuf_sizes[0] = obytes; | |
152 | ||
153 | iargs.numInSamples = ilen ? ilen : -1; /* -1 for signaling EOF */ | |
154 | ibdesc.numBufs = 1; | |
155 | ibdesc.bufs = ibufs; | |
156 | ibdesc.bufferIdentifiers = ibuf_ids; | |
157 | ibdesc.bufSizes = ibuf_sizes; | |
158 | ibdesc.bufElSizes = ibuf_el_sizes; | |
159 | obdesc.numBufs = 1; | |
160 | obdesc.bufs = obufs; | |
161 | obdesc.bufferIdentifiers = obuf_ids; | |
162 | obdesc.bufSizes = obuf_sizes; | |
163 | obdesc.bufElSizes = obuf_el_sizes; | |
164 | ||
165 | err = aacEncEncode(encoder, &ibdesc, &obdesc, &iargs, &oargs); | |
166 | if (err != AACENC_ENCODE_EOF && err != AACENC_OK) { | |
167 | fprintf(stderr, "ERROR: aacEncEncode() failed\n"); | |
168 | return -1; | |
169 | } | |
170 | *olen = oargs.numOutBytes; | |
171 | return oargs.numInSamples; | |
172 | } |