codecs.conf: remove yuv422 format from VDPAU section
[mplayer/glamo.git] / libmpcodecs / ae_faac.c
blob63c716378d5365e63dd3fc7e7b69c9d4586d3eab
1 /*
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.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <inttypes.h>
22 #include <unistd.h>
23 #include <string.h>
24 #include <sys/types.h>
25 #include "m_option.h"
26 #include "mp_msg.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"
33 #include <faac.h>
34 #include "ae.h"
37 static faacEncHandle faac;
38 static faacEncConfigurationPtr config = NULL;
39 static int
40 param_bitrate = 128,
41 param_quality = 0,
42 param_object_type = 1,
43 param_mpeg = 2,
44 param_tns = 0,
45 param_raw = 0,
46 param_cutoff = 0,
47 param_format = 16,
48 param_debug = 0;
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", &param_bitrate, CONF_TYPE_INT, 0, 0, 0, NULL},
57 {"quality", &param_quality, CONF_TYPE_INT, CONF_RANGE, 0, 1000, NULL},
58 {"object", &param_object_type, CONF_TYPE_INT, CONF_RANGE, 1, 4, NULL},
59 {"mpeg", &param_mpeg, CONF_TYPE_INT, CONF_RANGE, 2, 4, NULL},
60 {"tns", &param_tns, CONF_TYPE_FLAG, 0, 0, 1, NULL},
61 {"cutoff", &param_cutoff, CONF_TYPE_INT, 0, 0, 0, NULL},
62 {"format", &param_format, CONF_TYPE_INT, 0, 0, 0, NULL},
63 {"raw", &param_raw, CONF_TYPE_FLAG, 0, 0, 1, NULL},
64 {"debug", &param_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;
92 // Fix allocation
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;
99 else
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);
108 return 1;
111 static int get_frame_size(audio_encoder_t *encoder)
113 int sz = enc_frame_size;
114 enc_frame_size = 0;
115 return sz;
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)
134 return 1;
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);
142 return 0;
145 faac = faacEncOpen(encoder->params.sample_rate, encoder->params.channels, &samples_input, &max_bytes_output);
146 if(!faac)
148 mp_msg(MSGT_MENCODER, MSGL_FATAL, "AE_FAAC, couldn't init, exit\n");
149 return 0;
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);
153 if(!config)
155 mp_msg(MSGT_MENCODER, MSGL_FATAL, "AE_FAAC, couldn't get init configuration, exit\n");
156 return 0;
159 param_bitrate *= 1000;
160 if(param_quality)
161 config->quantqual = param_quality;
162 else
163 config->bitRate = param_bitrate / encoder->params.channels;
165 if(param_format==33)
167 config->inputFormat = FAAC_INPUT_FLOAT;
168 divisor = 4;
170 else if(param_format==32)
172 config->inputFormat = FAAC_INPUT_32BIT;
173 divisor = 4;
175 else
177 config->inputFormat = FAAC_INPUT_16BIT;
178 divisor = 2;
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)
192 config->useLfe = 1;
194 if(!faacEncSetConfiguration(faac, config))
196 mp_msg(MSGT_MENCODER, MSGL_FATAL, "AE_FAAC, counldn't set specified parameters, exiting\n");
197 return 0;
200 if(param_raw)
201 faacEncGetDecoderSpecificInfo(faac, &decoder_specific_buffer, &decoder_specific_len);
202 else
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;
214 return 1;