Improve VDPAU noforce-mixer documentation.
[mplayer.git] / libmpcodecs / ae_faac.c
blob3ed02d3f058800fdb91a297c8bb1d72d89386a8a
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 "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"
15 #include <faac.h>
16 #include "ae.h"
19 static faacEncHandle faac;
20 static faacEncConfigurationPtr config = NULL;
21 static int
22 param_bitrate = 128,
23 param_quality = 0,
24 param_object_type = 1,
25 param_mpeg = 2,
26 param_tns = 0,
27 param_raw = 0,
28 param_cutoff = 0,
29 param_format = 16,
30 param_debug = 0;
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", &param_bitrate, CONF_TYPE_INT, 0, 0, 0, NULL},
39 {"quality", &param_quality, CONF_TYPE_INT, CONF_RANGE, 0, 1000, NULL},
40 {"object", &param_object_type, CONF_TYPE_INT, CONF_RANGE, 1, 4, NULL},
41 {"mpeg", &param_mpeg, CONF_TYPE_INT, CONF_RANGE, 2, 4, NULL},
42 {"tns", &param_tns, CONF_TYPE_FLAG, 0, 0, 1, NULL},
43 {"cutoff", &param_cutoff, CONF_TYPE_INT, 0, 0, 0, NULL},
44 {"format", &param_format, CONF_TYPE_INT, 0, 0, 0, NULL},
45 {"raw", &param_raw, CONF_TYPE_FLAG, 0, 0, 1, NULL},
46 {"debug", &param_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;
74 // Fix allocation
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;
81 else
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);
90 return 1;
93 static int get_frame_size(audio_encoder_t *encoder)
95 int sz = enc_frame_size;
96 enc_frame_size = 0;
97 return sz;
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)
116 return 1;
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);
124 return 0;
127 faac = faacEncOpen(encoder->params.sample_rate, encoder->params.channels, &samples_input, &max_bytes_output);
128 if(!faac)
130 mp_msg(MSGT_MENCODER, MSGL_FATAL, "AE_FAAC, couldn't init, exit\n");
131 return 0;
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);
135 if(!config)
137 mp_msg(MSGT_MENCODER, MSGL_FATAL, "AE_FAAC, couldn't get init configuration, exit\n");
138 return 0;
141 param_bitrate *= 1000;
142 if(param_quality)
143 config->quantqual = param_quality;
144 else
145 config->bitRate = param_bitrate / encoder->params.channels;
147 if(param_format==33)
149 config->inputFormat = FAAC_INPUT_FLOAT;
150 divisor = 4;
152 else if(param_format==32)
154 config->inputFormat = FAAC_INPUT_32BIT;
155 divisor = 4;
157 else
159 config->inputFormat = FAAC_INPUT_16BIT;
160 divisor = 2;
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)
174 config->useLfe = 1;
176 if(!faacEncSetConfiguration(faac, config))
178 mp_msg(MSGT_MENCODER, MSGL_FATAL, "AE_FAAC, counldn't set specified parameters, exiting\n");
179 return 0;
182 if(param_raw)
183 faacEncGetDecoderSpecificInfo(faac, &decoder_specific_buffer, &decoder_specific_len);
184 else
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;
196 return 1;