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 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
|
/*
* Copyright 2008-2013 Various Authors
* Copyright 2006 dnk <dnk@bjum.net>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "ip.h"
#include "xmalloc.h"
#include "debug.h"
#include "id3.h"
#include "comment.h"
#include "read_wrapper.h"
#include "aac.h"
#include <neaacdec.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
/* FAAD_MIN_STREAMSIZE == 768, 6 == # of channels */
#define BUFFER_SIZE (FAAD_MIN_STREAMSIZE * 6 * 4)
struct aac_private {
char rbuf[BUFFER_SIZE];
int rbuf_len;
int rbuf_pos;
unsigned char channels;
unsigned long sample_rate;
long bitrate;
int object_type;
struct {
unsigned long samples;
unsigned long bytes;
} current;
char *overflow_buf;
int overflow_buf_len;
NeAACDecHandle decoder; /* typedef void * */
};
static inline int buffer_length(const struct input_plugin_data *ip_data)
{
struct aac_private *priv = ip_data->private;
return priv->rbuf_len - priv->rbuf_pos;
}
static inline void *buffer_data(const struct input_plugin_data *ip_data)
{
struct aac_private *priv = ip_data->private;
return priv->rbuf + priv->rbuf_pos;
}
static int buffer_fill(struct input_plugin_data *ip_data)
{
struct aac_private *priv = ip_data->private;
int32_t n;
if (priv->rbuf_pos > 0) {
priv->rbuf_len = buffer_length(ip_data);
memmove(priv->rbuf, priv->rbuf + priv->rbuf_pos, priv->rbuf_len);
priv->rbuf_pos = 0;
}
if (priv->rbuf_len == BUFFER_SIZE)
return 1;
n = read_wrapper(ip_data, priv->rbuf + priv->rbuf_len, BUFFER_SIZE - priv->rbuf_len);
if (n == -1)
return -1;
if (n == 0)
return 0;
priv->rbuf_len += n;
return 1;
}
static inline void buffer_consume(struct input_plugin_data *ip_data, int n)
{
struct aac_private *priv = ip_data->private;
BUG_ON(n > buffer_length(ip_data));
priv->rbuf_pos += n;
}
static int buffer_fill_min(struct input_plugin_data *ip_data, int len)
{
int rc;
BUG_ON(len > BUFFER_SIZE);
while (buffer_length(ip_data) < len) {
rc = buffer_fill(ip_data);
if (rc <= 0)
return rc;
}
return 1;
}
/* 'data' must point to at least 6 bytes of data */
static inline int parse_frame(const unsigned char data[6])
{
int len;
/* http://www.audiocoding.com/modules/wiki/?page=ADTS */
/* first 12 bits must be set */
if (data[0] != 0xFF)
return 0;
if ((data[1] & 0xF0) != 0xF0)
return 0;
/* layer is always '00' */
if ((data[1] & 0x06) != 0x00)
return 0;
/* frame length is stored in 13 bits */
len = data[3] << 11; /* ..1100000000000 */
len |= data[4] << 3; /* ..xx11111111xxx */
len |= data[5] >> 5; /* ..xxxxxxxxxx111 */
len &= 0x1FFF; /* 13 bits */
return len;
}
/* scans forward to the next aac frame and makes sure
* the entire frame is in the buffer.
*/
static int buffer_fill_frame(struct input_plugin_data *ip_data)
{
unsigned char *data;
int rc, n, len;
int max = 32768;
while (1) {
/* need at least 6 bytes of data */
rc = buffer_fill_min(ip_data, 6);
if (rc <= 0)
return rc;
len = buffer_length(ip_data);
data = buffer_data(ip_data);
/* scan for a frame */
for (n = 0; n < len - 5; n++) {
/* give up after 32KB */
if (max-- == 0) {
d_print("no frame found!\n");
/* FIXME: set errno? */
return -1;
}
/* see if there's a frame at this location */
rc = parse_frame(data + n);
if (rc == 0)
continue;
/* found a frame, consume all data up to the frame */
buffer_consume(ip_data, n);
/* rc == frame length */
rc = buffer_fill_min(ip_data, rc);
if (rc <= 0)
return rc;
return 1;
}
/* consume what we used */
buffer_consume(ip_data, n);
}
/* not reached */
}
static void aac_get_channel_map(struct input_plugin_data *ip_data)
{
struct aac_private *priv = ip_data->private;
NeAACDecFrameInfo frame_info;
void *buf;
int i;
ip_data->channel_map[0] = CHANNEL_POSITION_INVALID;
if (buffer_fill_frame(ip_data) <= 0)
return;
buf = NeAACDecDecode(priv->decoder, &frame_info, buffer_data(ip_data), buffer_length(ip_data));
if (!buf || frame_info.error != 0 || frame_info.bytesconsumed <= 0
|| frame_info.channels > CHANNELS_MAX)
return;
for (i = 0; i < frame_info.channels; i++)
ip_data->channel_map[i] = channel_position_aac(frame_info.channel_position[i]);
}
static int aac_open(struct input_plugin_data *ip_data)
{
struct aac_private *priv;
NeAACDecConfigurationPtr neaac_cfg;
int ret, n;
/* init private struct */
const struct aac_private priv_init = {
.decoder = NeAACDecOpen(),
.bitrate = -1,
.object_type = -1
};
priv = xnew(struct aac_private, 1);
*priv = priv_init;
ip_data->private = priv;
/* set decoder config */
neaac_cfg = NeAACDecGetCurrentConfiguration(priv->decoder);
neaac_cfg->outputFormat = FAAD_FMT_16BIT; /* force 16 bit audio */
neaac_cfg->downMatrix = 0; /* NOT 5.1 -> stereo */
neaac_cfg->dontUpSampleImplicitSBR = 0; /* upsample, please! */
NeAACDecSetConfiguration(priv->decoder, neaac_cfg);
/* find a frame */
if (buffer_fill_frame(ip_data) <= 0) {
ret = -IP_ERROR_FILE_FORMAT;
goto out;
}
/* in case of a bug, make sure there is at least some data
* in the buffer for NeAACDecInit() to work with.
*/
if (buffer_fill_min(ip_data, 256) <= 0) {
d_print("not enough data\n");
ret = -IP_ERROR_FILE_FORMAT;
goto out;
}
/* init decoder, returns the length of the header (if any) */
n = NeAACDecInit(priv->decoder, buffer_data(ip_data), buffer_length(ip_data),
&priv->sample_rate, &priv->channels);
if (n < 0) {
d_print("NeAACDecInit failed\n");
ret = -IP_ERROR_FILE_FORMAT;
goto out;
}
d_print("sample rate %luhz, channels %u\n", priv->sample_rate, priv->channels);
if (!priv->sample_rate || !priv->channels) {
ret = -IP_ERROR_FILE_FORMAT;
goto out;
}
/* skip the header */
d_print("skipping header (%d bytes)\n", n);
buffer_consume(ip_data, n);
/*NeAACDecInitDRM(priv->decoder, priv->sample_rate, priv->channels);*/
ip_data->sf = sf_rate(priv->sample_rate) | sf_channels(priv->channels) | sf_bits(16) | sf_signed(1);
#if defined(WORDS_BIGENDIAN)
ip_data->sf |= sf_bigendian(1);
#endif
aac_get_channel_map(ip_data);
return 0;
out:
NeAACDecClose(priv->decoder);
free(priv);
return ret;
}
static int aac_close(struct input_plugin_data *ip_data)
{
struct aac_private *priv = ip_data->private;
NeAACDecClose(priv->decoder);
free(priv);
ip_data->private = NULL;
return 0;
}
/* returns -1 on fatal errors
* returns -2 on non-fatal errors
* 0 on eof
* number of bytes put in 'buffer' on success */
static int decode_one_frame(struct input_plugin_data *ip_data, void *buffer, int count)
{
struct aac_private *priv = ip_data->private;
unsigned char *aac_data;
unsigned int aac_data_size;
NeAACDecFrameInfo frame_info;
char *sample_buf;
int bytes, rc;
rc = buffer_fill_frame(ip_data);
if (rc <= 0)
return rc;
aac_data = buffer_data(ip_data);
aac_data_size = buffer_length(ip_data);
/* aac data -> raw pcm */
sample_buf = NeAACDecDecode(priv->decoder, &frame_info, aac_data, aac_data_size);
if (frame_info.error == 0 && frame_info.samples > 0) {
priv->current.samples += frame_info.samples;
priv->current.bytes += frame_info.bytesconsumed;
}
buffer_consume(ip_data, frame_info.bytesconsumed);
if (!sample_buf || frame_info.bytesconsumed <= 0) {
d_print("fatal error: %s\n", NeAACDecGetErrorMessage(frame_info.error));
errno = EINVAL;
return -1;
}
if (frame_info.error != 0) {
d_print("frame error: %s\n", NeAACDecGetErrorMessage(frame_info.error));
return -2;
}
if (frame_info.samples <= 0)
return -2;
if (frame_info.channels != priv->channels || frame_info.samplerate != priv->sample_rate) {
d_print("invalid channel or sample_rate count\n");
return -2;
}
/* 16-bit samples */
bytes = frame_info.samples * 2;
if (bytes > count) {
/* decoded too much, keep overflow */
priv->overflow_buf = sample_buf + count;
priv->overflow_buf_len = bytes - count;
memcpy(buffer, sample_buf, count);
return count;
} else {
memcpy(buffer, sample_buf, bytes);
}
return bytes;
}
static int aac_read(struct input_plugin_data *ip_data, char *buffer, int count)
{
struct aac_private *priv = ip_data->private;
int rc;
/* use overflow from previous call (if any) */
if (priv->overflow_buf_len) {
int len = priv->overflow_buf_len;
if (len > count)
len = count;
memcpy(buffer, priv->overflow_buf, len);
priv->overflow_buf += len;
priv->overflow_buf_len -= len;
return len;
}
do {
rc = decode_one_frame(ip_data, buffer, count);
} while (rc == -2);
return rc;
}
static int aac_seek(struct input_plugin_data *ip_data, double offset)
{
return -IP_ERROR_FUNCTION_NOT_SUPPORTED;
}
static int aac_read_comments(struct input_plugin_data *ip_data,
struct keyval **comments)
{
GROWING_KEYVALS(c);
struct id3tag id3;
int rc, fd, i;
fd = open(ip_data->filename, O_RDONLY);
if (fd == -1)
return -1;
id3_init(&id3);
rc = id3_read_tags(&id3, fd, ID3_V1 | ID3_V2);
if (rc == -1) {
d_print("error: %s\n", strerror(errno));
goto out;
}
for (i = 0; i < NUM_ID3_KEYS; i++) {
char *val = id3_get_comment(&id3, i);
if (val)
comments_add(&c, id3_key_names[i], val);
}
out:
close(fd);
id3_free(&id3);
keyvals_terminate(&c);
*comments = c.keyvals;
return 0;
}
static int aac_duration(struct input_plugin_data *ip_data)
{
struct aac_private *priv = ip_data->private;
NeAACDecFrameInfo frame_info;
int samples = 0, bytes = 0, frames = 0;
off_t file_size;
file_size = lseek(ip_data->fd, 0, SEEK_END);
if (file_size == -1)
return -IP_ERROR_FUNCTION_NOT_SUPPORTED;
/* Seek to the middle of the file. There is almost always silence at
* the beginning, which gives wrong results. */
if (lseek(ip_data->fd, file_size/2, SEEK_SET) == -1)
return -IP_ERROR_FUNCTION_NOT_SUPPORTED;
priv->rbuf_pos = 0;
priv->rbuf_len = 0;
/* guess track length by decoding the first 10 frames */
while (frames < 10) {
if (buffer_fill_frame(ip_data) <= 0)
break;
NeAACDecDecode(priv->decoder, &frame_info,
buffer_data(ip_data), buffer_length(ip_data));
if (frame_info.error == 0 && frame_info.samples > 0) {
samples += frame_info.samples;
bytes += frame_info.bytesconsumed;
frames++;
}
if (frame_info.bytesconsumed == 0)
break;
buffer_consume(ip_data, frame_info.bytesconsumed);
}
if (frames == 0)
return -IP_ERROR_FUNCTION_NOT_SUPPORTED;
samples /= frames;
samples /= priv->channels;
bytes /= frames;
/* 8 * file_size / duration */
priv->bitrate = (8 * bytes * priv->sample_rate) / samples;
priv->object_type = frame_info.object_type;
return ((file_size / bytes) * samples) / priv->sample_rate;
}
static long aac_bitrate(struct input_plugin_data *ip_data)
{
struct aac_private *priv = ip_data->private;
return priv->bitrate != -1 ? priv->bitrate : -IP_ERROR_FUNCTION_NOT_SUPPORTED;
}
static long aac_current_bitrate(struct input_plugin_data *ip_data)
{
struct aac_private *priv = ip_data->private;
long bitrate = -1;
if (priv->current.samples > 0) {
priv->current.samples /= priv->channels;
bitrate = (8 * priv->current.bytes * priv->sample_rate) / priv->current.samples;
priv->current.samples = 0;
priv->current.bytes = 0;
}
return bitrate;
}
static char *aac_codec(struct input_plugin_data *ip_data)
{
return xstrdup("aac");
}
static const char *object_type_to_str(int object_type)
{
switch (object_type) {
case MAIN: return "Main";
case LC: return "LC";
case SSR: return "SSR";
case LTP: return "LTP";
case HE_AAC: return "HE";
case ER_LC: return "ER-LD";
case ER_LTP: return "ER-LTP";
case LD: return "LD";
case DRM_ER_LC: return "DRM-ER-LC";
}
return NULL;
}
static char *aac_codec_profile(struct input_plugin_data *ip_data)
{
struct aac_private *priv = ip_data->private;
const char *profile = object_type_to_str(priv->object_type);
return profile ? xstrdup(profile) : NULL;
}
const struct input_plugin_ops ip_ops = {
.open = aac_open,
.close = aac_close,
.read = aac_read,
.seek = aac_seek,
.read_comments = aac_read_comments,
.duration = aac_duration,
.bitrate = aac_bitrate,
.bitrate_current = aac_current_bitrate,
.codec = aac_codec,
.codec_profile = aac_codec_profile
};
const int ip_priority = 50;
const char * const ip_extensions[] = { "aac", NULL };
const char * const ip_mime_types[] = { "audio/aac", "audio/aacp", NULL };
const char * const ip_options[] = { NULL };
|