10 #include "libaf/af_format.h"
17 static faacEncHandle faac
;
18 static faacEncConfigurationPtr config
= NULL
;
22 param_object_type
= MAIN
,
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;
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
}
49 static int bind_faac(audio_encoder_t
*encoder
, muxer_stream_t
*mux_a
)
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
;
58 mux_a
->wf
->nAvgBytesPerSec
= encoder
->params
.bitrate
/ 8;
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
;
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;
73 mux_a
->wf
= realloc(mux_a
->wf
, sizeof(WAVEFORMATEX
)+mux_a
->wf
->cbSize
);
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
;
80 encoder
->input_format
= AF_FORMAT_S16_NE
;
82 encoder
->min_buffer_size
= mux_a
->h
.dwSuggestedBufferSize
;
83 encoder
->max_buffer_size
= mux_a
->h
.dwSuggestedBufferSize
*2;
85 if(decoder_specific_buffer
&& decoder_specific_len
)
86 memcpy(mux_a
->wf
+ 1, decoder_specific_buffer
, decoder_specific_len
);
91 static int get_frame_size(audio_encoder_t
*encoder
)
93 int sz
= enc_frame_size
;
98 static int encode_faac(audio_encoder_t
*encoder
, uint8_t *dest
, void *src
, int len
, int max_size
)
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
);
103 return enc_frame_size
;
106 int close_faac(audio_encoder_t
*encoder
)
111 int mpae_init_faac(audio_encoder_t
*encoder
)
113 if(encoder
->params
.channels
< 1 || encoder
->params
.channels
> 6 || (param_mpeg
!= 2 && param_mpeg
!= 4))
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
);
119 faac
= faacEncOpen(encoder
->params
.sample_rate
, encoder
->params
.channels
, &samples_input
, &max_bytes_output
);
122 mp_msg(MSGT_MENCODER
, MSGL_FATAL
, "AE_FAAC, couldn't init, exit\n");
125 mp_msg(MSGT_MENCODER
, MSGL_V
, "AE_FAAC, sample_input: %lu, max_bytes_output: %lu\n", samples_input
, max_bytes_output
);
126 config
= faacEncGetCurrentConfiguration(faac
);
129 mp_msg(MSGT_MENCODER
, MSGL_FATAL
, "AE_FAAC, couldn't get init configuration, exit\n");
133 param_bitrate
*= 1000;
135 config
->quantqual
= param_quality
;
137 config
->bitRate
= param_bitrate
/ encoder
->params
.channels
;
141 config
->inputFormat
= FAAC_INPUT_FLOAT
;
144 else if(param_format
==32)
146 config
->inputFormat
= FAAC_INPUT_32BIT
;
151 config
->inputFormat
= FAAC_INPUT_16BIT
;
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)
167 if(!faacEncSetConfiguration(faac
, config
))
169 mp_msg(MSGT_MENCODER
, MSGL_FATAL
, "AE_FAAC, counldn't set specified parameters, exiting\n");
174 faacEncGetDecoderSpecificInfo(faac
, &decoder_specific_buffer
, &decoder_specific_len
);
176 decoder_specific_len
= 0;
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
182 encoder
->bind
= bind_faac
;
183 encoder
->get_frame_size
= get_frame_size
;
184 encoder
->encode
= encode_faac
;
185 encoder
->close
= close_faac
;