15259
|
1 #include <stdio.h>
|
|
2 #include <stdlib.h>
|
|
3 #include <inttypes.h>
|
|
4 #include <unistd.h>
|
|
5 #include <string.h>
|
|
6 #include <sys/types.h>
|
|
7 #include "m_option.h"
|
|
8 #include "../mp_msg.h"
|
|
9 #include "aviheader.h"
|
|
10 #include "../libaf/af_format.h"
|
|
11 #include "ms_hdr.h"
|
|
12 #include "muxer.h"
|
|
13 #include <faac.h>
|
|
14 #include "ae.h"
|
|
15
|
|
16
|
|
17 static faacEncHandle faac;
|
|
18 static faacEncConfigurationPtr config = NULL;
|
|
19 static int
|
|
20 param_bitrate = 128,
|
|
21 param_quality = 0,
|
|
22 param_object_type = MAIN,
|
|
23 param_mpeg = 2,
|
|
24 param_tns = 0,
|
|
25 param_raw = 0,
|
|
26 param_cutoff = 0,
|
|
27 param_format = 16,
|
|
28 param_debug = 0;
|
|
29
|
|
30 static int enc_frame_size = 0, divisor;
|
|
31 static unsigned long samples_input, max_bytes_output;
|
|
32 static unsigned char *decoder_specific_buffer = NULL;
|
|
33 static unsigned long decoder_specific_len = 0;
|
|
34
|
|
35 m_option_t faacopts_conf[] = {
|
|
36 {"br", ¶m_bitrate, CONF_TYPE_INT, 0, 0, 0, NULL},
|
|
37 {"quality", ¶m_quality, CONF_TYPE_INT, CONF_RANGE, 0, 1000, NULL},
|
|
38 {"object", ¶m_object_type, CONF_TYPE_INT, CONF_RANGE, MAIN, LTP, NULL},
|
|
39 {"mpeg", ¶m_mpeg, CONF_TYPE_INT, CONF_RANGE, 2, 4, NULL},
|
|
40 {"tns", ¶m_tns, CONF_TYPE_FLAG, 0, 0, 1, NULL},
|
|
41 {"cutoff", ¶m_cutoff, CONF_TYPE_INT, 0, 0, 0, NULL},
|
|
42 {"format", ¶m_format, CONF_TYPE_INT, 0, 0, 0, NULL},
|
|
43 {"raw", ¶m_raw, CONF_TYPE_FLAG, 0, 0, 1, NULL},
|
|
44 {"debug", ¶m_debug, CONF_TYPE_INT, CONF_RANGE, 0, 100000000, NULL},
|
|
45 {NULL, NULL, 0, 0, 0, 0, NULL}
|
|
46 };
|
|
47
|
|
48
|
|
49 static int bind_faac(audio_encoder_t *encoder, muxer_stream_t *mux_a)
|
|
50 {
|
|
51 mux_a->wf = calloc(1, sizeof(WAVEFORMATEX) + decoder_specific_len + 256);
|
|
52 mux_a->wf->wFormatTag = 0x706D;
|
|
53 mux_a->wf->nChannels = encoder->params.channels;
|
|
54 mux_a->h.dwSampleSize=0; // VBR
|
|
55 mux_a->h.dwRate=encoder->params.sample_rate;
|
|
56 mux_a->h.dwScale=encoder->params.samples_per_frame;
|
|
57 mux_a->wf->nSamplesPerSec=mux_a->h.dwRate;
|
15276
|
58 mux_a->wf->nAvgBytesPerSec = encoder->params.bitrate / 8;
|
15259
|
59
|
|
60 mux_a->wf->nBlockAlign = mux_a->h.dwScale;
|
|
61 mux_a->h.dwSuggestedBufferSize = (encoder->params.audio_preload*mux_a->wf->nAvgBytesPerSec)/1000;
|
|
62 mux_a->h.dwSuggestedBufferSize -= mux_a->h.dwSuggestedBufferSize % mux_a->wf->nBlockAlign;
|
|
63
|
|
64 mux_a->wf->cbSize = decoder_specific_len;
|
|
65 mux_a->wf->wBitsPerSample = 0; /* does not apply */
|
|
66 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->wID = 1;
|
|
67 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->fdwFlags = 2;
|
|
68 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nBlockSize = mux_a->wf->nBlockAlign;
|
|
69 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nFramesPerBlock = 1;
|
|
70 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nCodecDelay = 0;
|
|
71
|
|
72 // Fix allocation
|
|
73 mux_a->wf = realloc(mux_a->wf, sizeof(WAVEFORMATEX)+mux_a->wf->cbSize);
|
|
74
|
|
75 if(config->inputFormat == FAAC_INPUT_FLOAT)
|
|
76 encoder->input_format = AF_FORMAT_FLOAT_NE;
|
|
77 else if(config->inputFormat == FAAC_INPUT_32BIT)
|
|
78 encoder->input_format = AF_FORMAT_S32_NE;
|
|
79 else
|
|
80 encoder->input_format = AF_FORMAT_S16_NE;
|
|
81
|
|
82 encoder->min_buffer_size = mux_a->h.dwSuggestedBufferSize;
|
|
83 encoder->max_buffer_size = mux_a->h.dwSuggestedBufferSize*2;
|
|
84
|
|
85 if(decoder_specific_buffer && decoder_specific_len)
|
|
86 memcpy(mux_a->wf + 1, decoder_specific_buffer, decoder_specific_len);
|
|
87
|
|
88 return 1;
|
|
89 }
|
|
90
|
|
91 static int get_frame_size(audio_encoder_t *encoder)
|
|
92 {
|
|
93 int sz = enc_frame_size;
|
|
94 enc_frame_size = 0;
|
|
95 return sz;
|
|
96 }
|
|
97
|
|
98 static int encode_faac(audio_encoder_t *encoder, uint8_t *dest, void *src, int len, int max_size)
|
|
99 {
|
|
100 // len is divided by the number of bytes per sample
|
|
101 enc_frame_size = faacEncEncode(faac, (int32_t*) src, len / divisor, dest, max_size);
|
|
102
|
|
103 return enc_frame_size;
|
|
104 }
|
|
105
|
|
106 int close_faac(audio_encoder_t *encoder)
|
|
107 {
|
|
108 return 1;
|
|
109 }
|
|
110
|
|
111 int mpae_init_faac(audio_encoder_t *encoder)
|
|
112 {
|
|
113 if(encoder->params.channels < 1 || encoder->params.channels > 6 || (param_mpeg != 2 && param_mpeg != 4))
|
|
114 {
|
|
115 mp_msg(MSGT_MENCODER, MSGL_FATAL, "AE_FAAC, unsupported number of channels: %d, or mpeg version: %d, exit\n", encoder->params.channels, param_mpeg);
|
|
116 return 0;
|
|
117 }
|
|
118
|
|
119 faac = faacEncOpen(encoder->params.sample_rate, encoder->params.channels, &samples_input, &max_bytes_output);
|
|
120 if(!faac)
|
|
121 {
|
|
122 mp_msg(MSGT_MENCODER, MSGL_FATAL, "AE_FAAC, couldn't init, exit\n");
|
|
123 return 0;
|
|
124 }
|
|
125 mp_msg(MSGT_MENCODER, MSGL_V, "AE_FAAC, sample_input: %u, max_bytes_output: %u\n", samples_input, max_bytes_output);
|
|
126 config = faacEncGetCurrentConfiguration(faac);
|
|
127 if(!config)
|
|
128 {
|
|
129 mp_msg(MSGT_MENCODER, MSGL_FATAL, "AE_FAAC, couldn't get init configuration, exit\n");
|
|
130 return 0;
|
|
131 }
|
|
132
|
|
133 param_bitrate *= 1000;
|
|
134 if(param_quality)
|
|
135 config->quantqual = param_quality;
|
|
136 else
|
|
137 config->bitRate = param_bitrate / encoder->params.channels;
|
|
138
|
|
139 if(param_format==33)
|
|
140 {
|
|
141 config->inputFormat = FAAC_INPUT_FLOAT;
|
|
142 divisor = 4;
|
|
143 }
|
|
144 else if(param_format==32)
|
|
145 {
|
|
146 config->inputFormat = FAAC_INPUT_32BIT;
|
|
147 divisor = 4;
|
|
148 }
|
|
149 else
|
|
150 {
|
|
151 config->inputFormat = FAAC_INPUT_16BIT;
|
|
152 divisor = 2;
|
|
153 }
|
|
154 config->outputFormat = param_raw ? 0 : 1; // 1 is ADTS
|
|
155 config->aacObjectType = param_object_type;
|
|
156 config->mpegVersion = (param_mpeg == 4 ? MPEG4 : MPEG2);
|
|
157 config->useTns = param_tns;
|
|
158 config->allowMidside = 1;
|
|
159 config->shortctl = SHORTCTL_NORMAL;
|
|
160 param_cutoff = param_cutoff ? param_cutoff : encoder->params.sample_rate / 2;
|
|
161 if(param_cutoff > encoder->params.sample_rate / 2)
|
|
162 param_cutoff = encoder->params.sample_rate / 2;
|
|
163 config->bandWidth = param_cutoff;
|
|
164 if(encoder->params.channels == 6)
|
|
165 config->useLfe = 1;
|
|
166
|
|
167 if(!faacEncSetConfiguration(faac, config))
|
|
168 {
|
|
169 mp_msg(MSGT_MENCODER, MSGL_FATAL, "AE_FAAC, counldn't set specified parameters, exiting\n");
|
|
170 return 0;
|
|
171 }
|
|
172
|
|
173 if(param_raw)
|
|
174 faacEncGetDecoderSpecificInfo(faac, &decoder_specific_buffer, &decoder_specific_len);
|
|
175 else
|
|
176 decoder_specific_len = 0;
|
|
177
|
|
178 encoder->params.bitrate = param_bitrate;
|
|
179 encoder->params.samples_per_frame = 1024;
|
|
180 encoder->decode_buffer_size = divisor * samples_input; //samples * 16 bits_per_sample
|
|
181
|
|
182 encoder->bind = bind_faac;
|
|
183 encoder->get_frame_size = get_frame_size;
|
|
184 encoder->encode = encode_faac;
|
|
185 encoder->close = close_faac;
|
|
186
|
|
187 return 1;
|
|
188 }
|