1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
|
/*
* encode_faac.c - encode audio frames using FAAC
* Written by Andrew Church <achurch@achurch.org>
*
* This file is part of transcode, a video stream processing tool.
* transcode is free software, distributable under the terms of the GNU
* General Public License (version 2 or later). See the file COPYING
* for details.
*/
#include "transcode.h"
#include "libtc/libtc.h"
#include "libtc/optstr.h"
#include "libtc/tcmodule-plugin.h"
#include <faac.h>
#define MOD_NAME "encode_faac.so"
#define MOD_VERSION "v0.1 (2006-10-11)"
#define MOD_CAP "Encodes audio to AAC using FAAC (currently BROKEN)"
#define MOD_AUTHOR "Andrew Church"
#define MOD_FEATURES \
TC_MODULE_FEATURE_ENCODE|TC_MODULE_FEATURE_AUDIO
#define MOD_FLAGS \
TC_MODULE_FLAG_RECONFIGURABLE
/*************************************************************************/
/* Local data structure: */
typedef struct {
faacEncHandle handle;
unsigned long framesize; // samples per AAC frame
int bps; // bytes per sample
/* FAAC only takes complete frames as input, so we buffer as needed. */
uint8_t *audiobuf;
int audiobuf_len; // in samples
} PrivateData;
/*************************************************************************/
/*************************************************************************/
/* Module interface routines and data. */
/*************************************************************************/
/**
* faacmod_init: Initialize this instance of the module. See
* tcmodule-data.h for function details.
*/
static int faac_init(TCModuleInstance *self, uint32_t features)
{
PrivateData *pd;
TC_MODULE_SELF_CHECK(self, "init");
TC_MODULE_INIT_CHECK(self, MOD_FEATURES, features);
self->userdata = pd = tc_malloc(sizeof(PrivateData));
if (!pd) {
tc_log_error(MOD_NAME, "init: out of memory!");
return TC_ERROR;
}
pd->handle = 0;
pd->audiobuf = NULL;
/* FIXME: shouldn't this test a specific flag? */
if (verbose) {
tc_log_info(MOD_NAME, "%s %s", MOD_VERSION, MOD_CAP);
if (verbose & TC_INFO) {
char *id, *copyright;
faacEncGetVersion(&id, ©right);
tc_log_info(MOD_NAME, "Using FAAC %s", id);
}
}
return TC_OK;
}
/*************************************************************************/
/**
* faac_configure: Configure this instance of the module. See
* tcmodule-data.h for function details.
*/
static int faac_configure(TCModuleInstance *self,
const char *options, vob_t *vob)
{
PrivateData *pd;
int samplerate = vob->mp3frequency ? vob->mp3frequency : vob->a_rate;
int ret;
unsigned long dummy;
faacEncConfiguration conf;
TC_MODULE_SELF_CHECK(self, "configure");
pd = self->userdata;
/* Save bytes per sample */
pd->bps = (vob->dm_chan * vob->dm_bits) / 8;
/* Create FAAC handle (freeing any old one that might be left over) */
if (pd->handle)
faacEncClose(pd->handle);
pd->handle = faacEncOpen(samplerate, vob->dm_chan, &pd->framesize, &dummy);
if (!pd->handle) {
tc_log_error(MOD_NAME, "FAAC initialization failed");
return TC_ERROR;
}
/* Set up our default audio parameters */
/* why can't just use a pointer here? -- FR */
/* Because the function returns a pointer to an internal buffer --AC */
conf = *faacEncGetCurrentConfiguration(pd->handle);
conf.mpegVersion = MPEG4;
conf.aacObjectType = MAIN;
conf.allowMidside = 1;
conf.useLfe = 0;
conf.useTns = 1;
conf.bitRate = vob->mp3bitrate / vob->dm_chan;
conf.bandWidth = 0; // automatic configuration
conf.quantqual = 100; // FIXME: quality should be a per-module setting
conf.outputFormat = 1;
if (vob->dm_bits != 16) {
tc_log_error(MOD_NAME, "Only 16-bit samples supported");
return TC_ERROR;
}
conf.inputFormat = FAAC_INPUT_16BIT;
conf.shortctl = SHORTCTL_NORMAL;
ret = optstr_get(options, "quality", "%li", &conf.quantqual);
if (ret >= 0) {
if (verbose >= TC_INFO) {
tc_log_info(MOD_NAME, "using quality=%li", conf.quantqual);
}
}
if (!faacEncSetConfiguration(pd->handle, &conf)) {
tc_log_error(MOD_NAME, "Failed to set FAAC configuration");
faacEncClose(pd->handle);
pd->handle = 0;
return TC_ERROR;
}
/* Allocate local audio buffer */
if (pd->audiobuf)
free(pd->audiobuf);
pd->audiobuf = tc_malloc(pd->framesize * pd->bps);
if (!pd->audiobuf) {
tc_log_error(MOD_NAME, "Unable to allocate audio buffer");
faacEncClose(pd->handle);
pd->handle = 0;
return TC_ERROR;
}
return TC_OK;
}
/*************************************************************************/
/**
* faac_inspect: Return the value of an option in this instance of the
* module. See tcmodule-data.h for function details.
*/
static int faac_inspect(TCModuleInstance *self,
const char *param, const char **value)
{
static char buf[TC_BUF_MAX];
TC_MODULE_SELF_CHECK(self, "inspect");
TC_MODULE_SELF_CHECK(param, "inspect");
if (optstr_lookup(param, "help")) {
tc_snprintf(buf, sizeof(buf),
"Overview:\n"
" Encodes audio to AAC using the FAAC library.\n"
"Options:\n"
" quality: set encoder quality [0-100]\n");
*value = buf;
}
return TC_OK;
}
/*************************************************************************/
/**
* faac_stop: Reset this instance of the module. See tcmodule-data.h for
* function details.
*/
static int faac_stop(TCModuleInstance *self)
{
PrivateData *pd;
TC_MODULE_SELF_CHECK(self, "stop");
pd = self->userdata;
if (pd->handle) {
faacEncClose(pd->handle);
pd->handle = NULL;
}
return TC_OK;
}
/*************************************************************************/
/**
* faac_fini: Clean up after this instance of the module. See
* tcmodule-data.h for function details.
*/
static int faac_fini(TCModuleInstance *self)
{
TC_MODULE_SELF_CHECK(self, "fini");
faac_stop(self);
tc_free(self->userdata);
self->userdata = NULL;
return TC_OK;
}
/*************************************************************************/
/**
* faac_encode: Encode a frame of data. See tcmodule-data.h for
* function details.
*/
static int faac_encode(TCModuleInstance *self,
aframe_list_t *in, aframe_list_t *out)
{
PrivateData *pd;
uint8_t *inptr;
int nsamples;
TC_MODULE_SELF_CHECK(self, "encode");
pd = self->userdata;
if (in) {
inptr = in->audio_buf;
nsamples = in->audio_size / pd->bps;
} else {
inptr = NULL;
nsamples = 0;
}
out->audio_len = 0;
while (pd->audiobuf_len + nsamples >= pd->framesize) {
int res;
const int tocopy = (pd->framesize - pd->audiobuf_len) * pd->bps;
ac_memcpy(pd->audiobuf + pd->audiobuf_len*pd->bps, inptr, tocopy);
inptr += tocopy;
nsamples -= tocopy / pd->bps;
pd->audiobuf_len = 0;
res = faacEncEncode(pd->handle, (int32_t *)pd->audiobuf, pd->framesize,
out->audio_buf + out->audio_len,
out->audio_size - out->audio_len);
if (res > out->audio_size - out->audio_len) {
tc_log_error(MOD_NAME,
"Output buffer overflow! Try a lower bitrate.");
return TC_ERROR;
}
out->audio_len += res;
}
if (nsamples > 0) {
ac_memcpy(pd->audiobuf + pd->audiobuf_len*pd->bps, inptr,
nsamples*pd->bps);
pd->audiobuf_len += nsamples;
}
return TC_OK;
}
/*************************************************************************/
static const TCCodecID faac_codecs_in[] = { TC_CODEC_PCM, TC_CODEC_ERROR };
static const TCCodecID faac_codecs_out[] = { TC_CODEC_AAC, TC_CODEC_ERROR };
TC_MODULE_CODEC_FORMATS(faac);
TC_MODULE_INFO(faac);
static const TCModuleClass faac_class = {
TC_MODULE_CLASS_HEAD(faac),
.init = faac_init,
.fini = faac_fini,
.configure = faac_configure,
.stop = faac_stop,
.inspect = faac_inspect,
.encode_audio = faac_encode,
};
TC_MODULE_ENTRY_POINT(faac);
/*************************************************************************/
/*
* Local variables:
* c-file-style: "stroustrup"
* c-file-offsets: ((case-label . *) (statement-case-intro . *))
* indent-tabs-mode: nil
* End:
*
* vim: expandtab shiftwidth=4:
*/
|