2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <sys/types.h>
27 #include "libmpdemux/aviheader.h"
28 #include "libaf/af_format.h"
29 #include "libaf/reorder_ch.h"
30 #include "libmpdemux/ms_hdr.h"
31 #include "stream/stream.h"
32 #include "libmpdemux/muxer.h"
37 static faacEncHandle faac
;
38 static faacEncConfigurationPtr config
= NULL
;
42 param_object_type
= 1,
50 static int enc_frame_size
= 0, divisor
;
51 static unsigned long samples_input
, max_bytes_output
;
52 static unsigned char *decoder_specific_buffer
= NULL
;
53 static unsigned long decoder_specific_len
= 0;
55 const m_option_t faacopts_conf
[] = {
56 {"br", ¶m_bitrate
, CONF_TYPE_INT
, 0, 0, 0, NULL
},
57 {"quality", ¶m_quality
, CONF_TYPE_INT
, CONF_RANGE
, 0, 1000, NULL
},
58 {"object", ¶m_object_type
, CONF_TYPE_INT
, CONF_RANGE
, 1, 4, NULL
},
59 {"mpeg", ¶m_mpeg
, CONF_TYPE_INT
, CONF_RANGE
, 2, 4, NULL
},
60 {"tns", ¶m_tns
, CONF_TYPE_FLAG
, 0, 0, 1, NULL
},
61 {"cutoff", ¶m_cutoff
, CONF_TYPE_INT
, 0, 0, 0, NULL
},
62 {"format", ¶m_format
, CONF_TYPE_INT
, 0, 0, 0, NULL
},
63 {"raw", ¶m_raw
, CONF_TYPE_FLAG
, 0, 0, 1, NULL
},
64 {"debug", ¶m_debug
, CONF_TYPE_INT
, CONF_RANGE
, 0, 100000000, NULL
},
65 {NULL
, NULL
, 0, 0, 0, 0, NULL
}
69 static int bind_faac(audio_encoder_t
*encoder
, muxer_stream_t
*mux_a
)
71 mux_a
->wf
= calloc(1, sizeof(WAVEFORMATEX
) + decoder_specific_len
+ 256);
72 mux_a
->wf
->wFormatTag
= 0x706D;
73 mux_a
->wf
->nChannels
= encoder
->params
.channels
;
74 mux_a
->h
.dwSampleSize
=0; // VBR
75 mux_a
->h
.dwRate
=encoder
->params
.sample_rate
;
76 mux_a
->h
.dwScale
=encoder
->params
.samples_per_frame
;
77 mux_a
->wf
->nSamplesPerSec
=mux_a
->h
.dwRate
;
78 mux_a
->wf
->nAvgBytesPerSec
= encoder
->params
.bitrate
/ 8;
80 mux_a
->wf
->nBlockAlign
= mux_a
->h
.dwScale
;
81 mux_a
->h
.dwSuggestedBufferSize
= (encoder
->params
.audio_preload
*mux_a
->wf
->nAvgBytesPerSec
)/1000;
82 mux_a
->h
.dwSuggestedBufferSize
-= mux_a
->h
.dwSuggestedBufferSize
% mux_a
->wf
->nBlockAlign
;
84 mux_a
->wf
->cbSize
= decoder_specific_len
;
85 mux_a
->wf
->wBitsPerSample
= 0; /* does not apply */
86 ((MPEGLAYER3WAVEFORMAT
*) (mux_a
->wf
))->wID
= 1;
87 ((MPEGLAYER3WAVEFORMAT
*) (mux_a
->wf
))->fdwFlags
= 2;
88 ((MPEGLAYER3WAVEFORMAT
*) (mux_a
->wf
))->nBlockSize
= mux_a
->wf
->nBlockAlign
;
89 ((MPEGLAYER3WAVEFORMAT
*) (mux_a
->wf
))->nFramesPerBlock
= 1;
90 ((MPEGLAYER3WAVEFORMAT
*) (mux_a
->wf
))->nCodecDelay
= 0;
93 mux_a
->wf
= realloc(mux_a
->wf
, sizeof(WAVEFORMATEX
)+mux_a
->wf
->cbSize
);
95 if(config
->inputFormat
== FAAC_INPUT_FLOAT
)
96 encoder
->input_format
= AF_FORMAT_FLOAT_NE
;
97 else if(config
->inputFormat
== FAAC_INPUT_32BIT
)
98 encoder
->input_format
= AF_FORMAT_S32_NE
;
100 encoder
->input_format
= AF_FORMAT_S16_NE
;
102 encoder
->min_buffer_size
= mux_a
->h
.dwSuggestedBufferSize
;
103 encoder
->max_buffer_size
= mux_a
->h
.dwSuggestedBufferSize
*2;
105 if(decoder_specific_buffer
&& decoder_specific_len
)
106 memcpy(mux_a
->wf
+ 1, decoder_specific_buffer
, decoder_specific_len
);
111 static int get_frame_size(audio_encoder_t
*encoder
)
113 int sz
= enc_frame_size
;
118 static int encode_faac(audio_encoder_t
*encoder
, uint8_t *dest
, void *src
, int len
, int max_size
)
120 if (encoder
->params
.channels
>= 5)
121 reorder_channel_nch(src
, AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT
,
122 AF_CHANNEL_LAYOUT_AAC_DEFAULT
,
123 encoder
->params
.channels
,
124 len
/ divisor
, divisor
);
126 // len is divided by the number of bytes per sample
127 enc_frame_size
= faacEncEncode(faac
, (int32_t*) src
, len
/ divisor
, dest
, max_size
);
129 return enc_frame_size
;
132 int close_faac(audio_encoder_t
*encoder
)
137 int mpae_init_faac(audio_encoder_t
*encoder
)
139 if(encoder
->params
.channels
< 1 || encoder
->params
.channels
> 6 || (param_mpeg
!= 2 && param_mpeg
!= 4))
141 mp_msg(MSGT_MENCODER
, MSGL_FATAL
, "AE_FAAC, unsupported number of channels: %d, or mpeg version: %d, exit\n", encoder
->params
.channels
, param_mpeg
);
145 faac
= faacEncOpen(encoder
->params
.sample_rate
, encoder
->params
.channels
, &samples_input
, &max_bytes_output
);
148 mp_msg(MSGT_MENCODER
, MSGL_FATAL
, "AE_FAAC, couldn't init, exit\n");
151 mp_msg(MSGT_MENCODER
, MSGL_V
, "AE_FAAC, sample_input: %lu, max_bytes_output: %lu\n", samples_input
, max_bytes_output
);
152 config
= faacEncGetCurrentConfiguration(faac
);
155 mp_msg(MSGT_MENCODER
, MSGL_FATAL
, "AE_FAAC, couldn't get init configuration, exit\n");
159 param_bitrate
*= 1000;
161 config
->quantqual
= param_quality
;
163 config
->bitRate
= param_bitrate
/ encoder
->params
.channels
;
167 config
->inputFormat
= FAAC_INPUT_FLOAT
;
170 else if(param_format
==32)
172 config
->inputFormat
= FAAC_INPUT_32BIT
;
177 config
->inputFormat
= FAAC_INPUT_16BIT
;
180 config
->outputFormat
= param_raw
? 0 : 1; // 1 is ADTS
181 config
->aacObjectType
= param_object_type
;
182 if(MAIN
==0) config
->aacObjectType
--;
183 config
->mpegVersion
= (param_mpeg
== 4 ? MPEG4
: MPEG2
);
184 config
->useTns
= param_tns
;
185 config
->allowMidside
= 1;
186 config
->shortctl
= SHORTCTL_NORMAL
;
187 param_cutoff
= param_cutoff
? param_cutoff
: encoder
->params
.sample_rate
/ 2;
188 if(param_cutoff
> encoder
->params
.sample_rate
/ 2)
189 param_cutoff
= encoder
->params
.sample_rate
/ 2;
190 config
->bandWidth
= param_cutoff
;
191 if(encoder
->params
.channels
== 6)
194 if(!faacEncSetConfiguration(faac
, config
))
196 mp_msg(MSGT_MENCODER
, MSGL_FATAL
, "AE_FAAC, counldn't set specified parameters, exiting\n");
201 faacEncGetDecoderSpecificInfo(faac
, &decoder_specific_buffer
, &decoder_specific_len
);
203 decoder_specific_len
= 0;
205 encoder
->params
.bitrate
= param_bitrate
;
206 encoder
->params
.samples_per_frame
= 1024;
207 encoder
->decode_buffer_size
= divisor
* samples_input
; //samples * 16 bits_per_sample
209 encoder
->bind
= bind_faac
;
210 encoder
->get_frame_size
= get_frame_size
;
211 encoder
->encode
= encode_faac
;
212 encoder
->close
= close_faac
;