9 #include "libmpdemux/aviheader.h"
10 #include "libaf/af_format.h"
11 #include "libaf/reorder_ch.h"
12 #include "libmpdemux/ms_hdr.h"
13 #include "stream/stream.h"
14 #include "libmpdemux/muxer.h"
19 static faacEncHandle faac
;
20 static faacEncConfigurationPtr config
= NULL
;
24 param_object_type
= 1,
32 static int enc_frame_size
= 0, divisor
;
33 static unsigned long samples_input
, max_bytes_output
;
34 static unsigned char *decoder_specific_buffer
= NULL
;
35 static unsigned long decoder_specific_len
= 0;
37 m_option_t faacopts_conf
[] = {
38 {"br", ¶m_bitrate
, CONF_TYPE_INT
, 0, 0, 0, NULL
},
39 {"quality", ¶m_quality
, CONF_TYPE_INT
, CONF_RANGE
, 0, 1000, NULL
},
40 {"object", ¶m_object_type
, CONF_TYPE_INT
, CONF_RANGE
, 1, 4, NULL
},
41 {"mpeg", ¶m_mpeg
, CONF_TYPE_INT
, CONF_RANGE
, 2, 4, NULL
},
42 {"tns", ¶m_tns
, CONF_TYPE_FLAG
, 0, 0, 1, NULL
},
43 {"cutoff", ¶m_cutoff
, CONF_TYPE_INT
, 0, 0, 0, NULL
},
44 {"format", ¶m_format
, CONF_TYPE_INT
, 0, 0, 0, NULL
},
45 {"raw", ¶m_raw
, CONF_TYPE_FLAG
, 0, 0, 1, NULL
},
46 {"debug", ¶m_debug
, CONF_TYPE_INT
, CONF_RANGE
, 0, 100000000, NULL
},
47 {NULL
, NULL
, 0, 0, 0, 0, NULL
}
51 static int bind_faac(audio_encoder_t
*encoder
, muxer_stream_t
*mux_a
)
53 mux_a
->wf
= calloc(1, sizeof(WAVEFORMATEX
) + decoder_specific_len
+ 256);
54 mux_a
->wf
->wFormatTag
= 0x706D;
55 mux_a
->wf
->nChannels
= encoder
->params
.channels
;
56 mux_a
->h
.dwSampleSize
=0; // VBR
57 mux_a
->h
.dwRate
=encoder
->params
.sample_rate
;
58 mux_a
->h
.dwScale
=encoder
->params
.samples_per_frame
;
59 mux_a
->wf
->nSamplesPerSec
=mux_a
->h
.dwRate
;
60 mux_a
->wf
->nAvgBytesPerSec
= encoder
->params
.bitrate
/ 8;
62 mux_a
->wf
->nBlockAlign
= mux_a
->h
.dwScale
;
63 mux_a
->h
.dwSuggestedBufferSize
= (encoder
->params
.audio_preload
*mux_a
->wf
->nAvgBytesPerSec
)/1000;
64 mux_a
->h
.dwSuggestedBufferSize
-= mux_a
->h
.dwSuggestedBufferSize
% mux_a
->wf
->nBlockAlign
;
66 mux_a
->wf
->cbSize
= decoder_specific_len
;
67 mux_a
->wf
->wBitsPerSample
= 0; /* does not apply */
68 ((MPEGLAYER3WAVEFORMAT
*) (mux_a
->wf
))->wID
= 1;
69 ((MPEGLAYER3WAVEFORMAT
*) (mux_a
->wf
))->fdwFlags
= 2;
70 ((MPEGLAYER3WAVEFORMAT
*) (mux_a
->wf
))->nBlockSize
= mux_a
->wf
->nBlockAlign
;
71 ((MPEGLAYER3WAVEFORMAT
*) (mux_a
->wf
))->nFramesPerBlock
= 1;
72 ((MPEGLAYER3WAVEFORMAT
*) (mux_a
->wf
))->nCodecDelay
= 0;
75 mux_a
->wf
= realloc(mux_a
->wf
, sizeof(WAVEFORMATEX
)+mux_a
->wf
->cbSize
);
77 if(config
->inputFormat
== FAAC_INPUT_FLOAT
)
78 encoder
->input_format
= AF_FORMAT_FLOAT_NE
;
79 else if(config
->inputFormat
== FAAC_INPUT_32BIT
)
80 encoder
->input_format
= AF_FORMAT_S32_NE
;
82 encoder
->input_format
= AF_FORMAT_S16_NE
;
84 encoder
->min_buffer_size
= mux_a
->h
.dwSuggestedBufferSize
;
85 encoder
->max_buffer_size
= mux_a
->h
.dwSuggestedBufferSize
*2;
87 if(decoder_specific_buffer
&& decoder_specific_len
)
88 memcpy(mux_a
->wf
+ 1, decoder_specific_buffer
, decoder_specific_len
);
93 static int get_frame_size(audio_encoder_t
*encoder
)
95 int sz
= enc_frame_size
;
100 static int encode_faac(audio_encoder_t
*encoder
, uint8_t *dest
, void *src
, int len
, int max_size
)
102 if (encoder
->params
.channels
>= 5)
103 reorder_channel_nch(src
, AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT
,
104 AF_CHANNEL_LAYOUT_AAC_DEFAULT
,
105 encoder
->params
.channels
,
106 len
/ divisor
, divisor
);
108 // len is divided by the number of bytes per sample
109 enc_frame_size
= faacEncEncode(faac
, (int32_t*) src
, len
/ divisor
, dest
, max_size
);
111 return enc_frame_size
;
114 int close_faac(audio_encoder_t
*encoder
)
119 int mpae_init_faac(audio_encoder_t
*encoder
)
121 if(encoder
->params
.channels
< 1 || encoder
->params
.channels
> 6 || (param_mpeg
!= 2 && param_mpeg
!= 4))
123 mp_msg(MSGT_MENCODER
, MSGL_FATAL
, "AE_FAAC, unsupported number of channels: %d, or mpeg version: %d, exit\n", encoder
->params
.channels
, param_mpeg
);
127 faac
= faacEncOpen(encoder
->params
.sample_rate
, encoder
->params
.channels
, &samples_input
, &max_bytes_output
);
130 mp_msg(MSGT_MENCODER
, MSGL_FATAL
, "AE_FAAC, couldn't init, exit\n");
133 mp_msg(MSGT_MENCODER
, MSGL_V
, "AE_FAAC, sample_input: %lu, max_bytes_output: %lu\n", samples_input
, max_bytes_output
);
134 config
= faacEncGetCurrentConfiguration(faac
);
137 mp_msg(MSGT_MENCODER
, MSGL_FATAL
, "AE_FAAC, couldn't get init configuration, exit\n");
141 param_bitrate
*= 1000;
143 config
->quantqual
= param_quality
;
145 config
->bitRate
= param_bitrate
/ encoder
->params
.channels
;
149 config
->inputFormat
= FAAC_INPUT_FLOAT
;
152 else if(param_format
==32)
154 config
->inputFormat
= FAAC_INPUT_32BIT
;
159 config
->inputFormat
= FAAC_INPUT_16BIT
;
162 config
->outputFormat
= param_raw
? 0 : 1; // 1 is ADTS
163 config
->aacObjectType
= param_object_type
;
164 if(MAIN
==0) config
->aacObjectType
--;
165 config
->mpegVersion
= (param_mpeg
== 4 ? MPEG4
: MPEG2
);
166 config
->useTns
= param_tns
;
167 config
->allowMidside
= 1;
168 config
->shortctl
= SHORTCTL_NORMAL
;
169 param_cutoff
= param_cutoff
? param_cutoff
: encoder
->params
.sample_rate
/ 2;
170 if(param_cutoff
> encoder
->params
.sample_rate
/ 2)
171 param_cutoff
= encoder
->params
.sample_rate
/ 2;
172 config
->bandWidth
= param_cutoff
;
173 if(encoder
->params
.channels
== 6)
176 if(!faacEncSetConfiguration(faac
, config
))
178 mp_msg(MSGT_MENCODER
, MSGL_FATAL
, "AE_FAAC, counldn't set specified parameters, exiting\n");
183 faacEncGetDecoderSpecificInfo(faac
, &decoder_specific_buffer
, &decoder_specific_len
);
185 decoder_specific_len
= 0;
187 encoder
->params
.bitrate
= param_bitrate
;
188 encoder
->params
.samples_per_frame
= 1024;
189 encoder
->decode_buffer_size
= divisor
* samples_input
; //samples * 16 bits_per_sample
191 encoder
->bind
= bind_faac
;
192 encoder
->get_frame_size
= get_frame_size
;
193 encoder
->encode
= encode_faac
;
194 encoder
->close
= close_faac
;