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 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
|
// ------------------------------------------------------------------------
// audioio-mp3.cpp: Interface for mp3 decoders and encoders that support
// input/output using standard streams. Defaults to
// mpg123 and lame.
// Copyright (C) 1999-2006,2008,2009 Kai Vehmanen
// Note! Routines for parsing mp3 header information were taken from XMMS
// 1.2.5's mpg123 plugin. Improvements to parsing logic were
// contributed by Julian Dobson.
//
// Attributes:
// eca-style-version: 3 (see Ecasound Programmer's Guide)
//
// References:
// http://www.mp3-tech.org/programmer/frame_header.html
// http://www.mpg123.de/
//
// 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, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// ------------------------------------------------------------------------
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string>
#include <cmath>
#include <cstring>
#include <cstdlib> /* atol() */
#include <signal.h>
#include <unistd.h> /* stat() */
#include <sys/stat.h> /* stat() */
#include <sys/wait.h>
#include <kvu_inttypes.h>
#include <kvu_message_item.h>
#include <kvu_numtostr.h>
#include "audioio-mp3.h"
#include "audioio-mp3_impl.h"
#include "samplebuffer.h"
#include "audioio.h"
#include "eca-logger.h"
const char *default_input_cmd = "mpg123 --stereo -b 0 -q -s -k %o %f";
const char *default_output_cmd = "lame -b %B -s %S -r --big-endian -S - %f";
const long int default_output_bitrate = 128000;
std::string MP3FILE::conf_input_cmd = std::string(default_input_cmd);
std::string MP3FILE::conf_output_cmd = std::string(default_output_cmd);
long int MP3FILE::conf_default_output_bitrate = default_output_bitrate;
void MP3FILE::set_input_cmd(const std::string& value) { MP3FILE::conf_input_cmd = value; }
void MP3FILE::set_output_cmd(const std::string& value) { MP3FILE::conf_output_cmd = value; }
/***************************************************************
* Routines for parsing mp3 header information. Taken from XMMS
* 1.2.5's mpg123 plugin.
**************************************************************/
#define MAXFRAMESIZE 1792
#define MPG_MD_STEREO 0
#define MPG_MD_JOINT_STEREO 1
#define MPG_MD_DUAL_CHANNEL 2
#define MPG_MD_MONO 3
int tabsel_123[2][3][16] =
{
{
{0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448,},
{0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384,},
{0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320,}},
{
{0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256,},
{0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,},
{0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,}}
};
long mpg123_freqs[9] =
{44100, 48000, 32000, 22050, 24000, 16000, 11025, 12000, 8000};
struct frame
{
int stereo;
int jsbound;
int single;
int II_sblimit;
int down_sample_sblimit;
int lsf;
int mpeg25;
int down_sample;
int header_change;
int lay;
int error_protection;
int bitrate_index;
int sampling_frequency;
int padding;
int extension;
int mode;
int mode_ext;
int copyright;
int original;
int emphasis;
int framesize; /* computed framesize */
};
static bool mpg123_head_check(unsigned long head)
{
/* ref: http://www.mp3-tech.org/programmer/frame_header.html */
/* frame sync must be 0xffe (11bits) */
if ((head & 0xffe00000) != 0xffe00000) return false;
/* layer must be non-null (2bits) */
if (!((head >> 17) & 3)) return false;
/* invalid bitrate index: all-ones (4bit) */
if (((head >> 12) & 0xf) == 0xf) return false;
/* invalid bitrate index: null (4bit) */
if (!((head >> 12) & 0xf)) return false;
/* invalid srate index: all-ones (2bit) */
if (((head >> 10) & 0x3) == 0x3) return false;
#if 0
/* invalid: mpeg2/2.5, layer I, protection bit off */
if (((head >> 19) & 1) == 1 && ((head >> 17) & 3) == 3 && ((head >> 16) & 1) == 1) return false;
/* - mpeg version 1, CRC protection bit */
if ((head & 0xffff0000) == 0xfffe0000) return false;
#endif
return true;
}
static double mpg123_compute_bpf(struct frame *fr)
{
double bpf;
switch (fr->lay)
{
case 1:
bpf = tabsel_123[fr->lsf][0][fr->bitrate_index];
bpf *= 12000.0 * 4.0;
bpf /= mpg123_freqs[fr->sampling_frequency] << (fr->lsf);
break;
case 2:
case 3:
bpf = tabsel_123[fr->lsf][fr->lay - 1][fr->bitrate_index];
bpf *= 144000;
bpf /= mpg123_freqs[fr->sampling_frequency] << (fr->lsf);
break;
default:
bpf = 1.0;
}
return bpf;
}
static double mpg123_compute_tpf(struct frame *fr)
{
static int bs[4] =
{0, 384, 1152, 1152};
double tpf;
tpf = (double) bs[fr->lay];
tpf /= mpg123_freqs[fr->sampling_frequency] << (fr->lsf);
return tpf;
}
/*
* the code a header and write the information
* into the frame structure
*/
static bool mpg123_decode_header(struct frame *fr, unsigned long newhead)
{
if (newhead & (1 << 20))
{
fr->lsf = (newhead & (1 << 19)) ? 0x0 : 0x1;
fr->mpeg25 = 0;
}
else
{
fr->lsf = 1;
fr->mpeg25 = 1;
}
fr->lay = 4 - ((newhead >> 17) & 3);
if (fr->mpeg25)
{
fr->sampling_frequency = 6 + ((newhead >> 10) & 0x3);
}
else
fr->sampling_frequency = ((newhead >> 10) & 0x3) + (fr->lsf * 3);
fr->error_protection = ((newhead >> 16) & 0x1) ^ 0x1;
if (fr->mpeg25) /* allow Bitrate change for 2.5 ... */
fr->bitrate_index = ((newhead >> 12) & 0xf);
fr->bitrate_index = ((newhead >> 12) & 0xf);
fr->padding = ((newhead >> 9) & 0x1);
fr->extension = ((newhead >> 8) & 0x1);
fr->mode = ((newhead >> 6) & 0x3);
fr->mode_ext = ((newhead >> 4) & 0x3);
fr->copyright = ((newhead >> 3) & 0x1);
fr->original = ((newhead >> 2) & 0x1);
fr->emphasis = newhead & 0x3;
fr->stereo = (fr->mode == MPG_MD_MONO) ? 1 : 2;
if (!fr->bitrate_index) {
ECA_LOG_MSG(ECA_LOGGER::errors, "Invalid bitrate!");
return false;
}
int ssize = 0;
switch (fr->lay)
{
case 1:
// fr->do_layer = mpg123_do_layer1;
// mpg123_init_layer2();
fr->framesize = (long) tabsel_123[fr->lsf][0][fr->bitrate_index] * 12000;
fr->framesize /= mpg123_freqs[fr->sampling_frequency];
fr->framesize = ((fr->framesize + fr->padding) << 2) - 4;
break;
case 2:
// fr->do_layer = mpg123_do_layer2;
// mpg123_init_layer2();
fr->framesize = (long) tabsel_123[fr->lsf][1][fr->bitrate_index] * 144000;
fr->framesize /= mpg123_freqs[fr->sampling_frequency];
fr->framesize += fr->padding - 4;
break;
case 3:
// fr->do_layer = mpg123_do_layer3;
if (fr->lsf)
ssize = (fr->stereo == 1) ? 9 : 17;
else
ssize = (fr->stereo == 1) ? 17 : 32;
if (fr->error_protection)
ssize += 2;
fr->framesize = (long) tabsel_123[fr->lsf][2][fr->bitrate_index] * 144000;
fr->framesize /= mpg123_freqs[fr->sampling_frequency] << (fr->lsf);
fr->framesize = fr->framesize + fr->padding - 4;
break;
default:
return false;
}
if(fr->framesize > MAXFRAMESIZE) {
ECA_LOG_MSG(ECA_LOGGER::errors, "Invalid framesize!");
return false;
}
return true;
}
/* not used anymore, kaiv 2005/03 */
#if 0
static uint32_t convert_to_header(uint8_t * buf)
{
return (buf[0] << 24) + (buf[1] << 16) + (buf[2] << 8) + buf[3];
}
#endif
static bool mpg123_detect_by_content(const char* filename, struct frame* frp)
{
FILE *file;
uint8_t tmp[4]; /* room for the 32bit head */
uint32_t head = 0;
bool data_left = true;
bool header_found = false;
size_t offset = 0;
if((file = std::fopen(filename, "rb")) == NULL) {
ECA_LOG_MSG(ECA_LOGGER::errors, string("Unable to open file ") + filename + ".");
data_left = false;
}
/* search for headers in the first 262kB of data */
while(data_left == true && offset < (1<<18)) {
/* octet-by-octet search */
if (std::fread(tmp, 1, 1, file) != 1) {
ECA_LOG_MSG(ECA_LOGGER::errors, "End of mp3 file, no valid header data found.");
data_left = false;
break;
}
head <<= 8;
head |= tmp[0];
offset += 1;
if (offset > 3) {
/* verify the header and if ok, fetch mp3 parameters and store
them to 'frp' */
if (mpg123_head_check(head) && mpg123_decode_header(frp, head)) {
if (header_found == true) {
/* two headers found, stop searching */
data_left = false;
}
else {
/* after the first header is found, skip to the next
valid frame to verify that the first frame is not
dummy frame (id3 or something similar) */
if (std::fseek(file, frp->framesize, SEEK_CUR) != 0) {
data_left = false;
}
header_found = true;
}
ECA_LOG_MSG(ECA_LOGGER::user_objects, "Found mp3 header at offset " +
kvu_numtostr(static_cast<int>(offset - 4)));
}
}
}
return header_found;
}
/***************************************************************
* MP3FILE specific parts.
**************************************************************/
MP3FILE::MP3FILE(const std::string& name)
: finished_rep(false),
triggered_rep(false)
{
set_label(name);
filedes_rep = -1;
filehandle_rep = 0;
mono_input_rep = false;
pcm_rep = 1;
bitrate_rep = MP3FILE::conf_default_output_bitrate;
}
MP3FILE::~MP3FILE(void)
{
/* see notes in stop_io() */
clean_child(io_mode() == io_read ? true : false);
if (is_open() == true) {
close();
}
}
void MP3FILE::open(void) throw(AUDIO_IO::SETUP_ERROR &)
{
if (io_mode() == io_read) {
/* decoder supports: fixed channel count and sample format,
sample rate set by parsing mp3 header */
get_mp3_params(label());
}
else {
/* encoder supports: srate configurable, fixed channel
count and sample format */
set_channels(2);
set_sample_format(ECA_AUDIO_FORMAT::sfmt_s16_le);
/* note: 'lame' command-line syntax, and default related to them,
* have changed slightly in lame 3.98, so we need this hack
* to support both old and new versions. In the past,
* Ecasound wrote little-endian samples and used lame
* option "-x". Newer lame versions (3.97) introduced
* "--litle-endian" and "--big-endian", but these were
* buggy still in 3.97 (fixed in 3.98). And with 3.98,
* additional options (e.g. "-r") need to be passed, or
* otherwise lame will exit with an error.
*
* In addition to above problems, we also need to remember
* people updating to a newer Ecasound, but who do not update
* their custom 'lame' launch commands in
* ~/.ecasound/ecasoundrc (ecasound must continue to output
* little-endian samples by default).
*/
if (MP3FILE::conf_output_cmd.find("lame ") != std::string::npos &&
MP3FILE::conf_output_cmd.find(" --big-endian ") != std::string::npos) {
set_sample_format(ECA_AUDIO_FORMAT::sfmt_s16_be);
}
}
triggered_rep = false;
AUDIO_IO::open();
}
void MP3FILE::close(void)
{
if (pid_of_child() > 0) {
ECA_LOG_MSG(ECA_LOGGER::user_objects, "Cleaning child process pid=" + kvu_numtostr(pid_of_child()) + ".");
/* note: mp3 input/output can handle SIGTERM */
clean_child(true);
triggered_rep = false;
}
AUDIO_IO::close();
}
void MP3FILE::process_mono_fix(char* target_buffer, long int bytes) {
for(long int n = 0; n < bytes;) {
target_buffer[n + 2] = target_buffer[n];
target_buffer[n + 3] = target_buffer[n + 1];
n += 4;
}
}
long int MP3FILE::read_samples(void* target_buffer, long int samples)
{
if (triggered_rep != true) {
ECA_LOG_MSG(ECA_LOGGER::info, "WARNING: triggering an external program in real-time context");
triggered_rep = true;
fork_input_process();
}
bytes_rep = std::fread(target_buffer, 1, frame_size() * samples, filehandle_rep);
if (bytes_rep < samples * frame_size() || bytes_rep == 0) {
if (position_in_samples() == 0)
ECA_LOG_MSG(ECA_LOGGER::errors, "Can't start process \"" + MP3FILE::conf_input_cmd + "\". Please check your ~/.ecasound/ecasoundrc.");
finished_rep = true;
triggered_rep = false;
}
else
finished_rep = false;
last_position_rep += (bytes_rep / frame_size());
return bytes_rep / frame_size();
}
void MP3FILE::write_samples(void* target_buffer, long int samples)
{
if (triggered_rep != true) {
triggered_rep = true;
fork_output_process();
}
if (wait_for_child() != true) {
finished_rep = true;
triggered_rep = false;
ECA_LOG_MSG(ECA_LOGGER::errors, "Attempt to write after child process has terminated.");
}
else {
bytes_rep = ::write(filedes_rep, target_buffer, frame_size() * samples);
if (bytes_rep < frame_size() * samples) {
if (position_in_samples() == 0)
ECA_LOG_MSG(ECA_LOGGER::errors, "Can't start process \"" + MP3FILE::conf_output_cmd + "\". Please check your ~/.ecasound/ecasoundrc.");
else
ECA_LOG_MSG(ECA_LOGGER::errors,
"Error in writing to child process (to write "
+ kvu_numtostr(frame_size() * samples)
+ ", result "
+ kvu_numtostr(bytes_rep)
+ ").");
finished_rep = true;
}
else
finished_rep = false;
}
}
SAMPLE_SPECS::sample_pos_t MP3FILE::seek_position(SAMPLE_SPECS::sample_pos_t pos)
{
finished_rep = false;
if (triggered_rep == true &&
last_position_rep != pos) {
if (is_open() == true) {
ECA_LOG_MSG(ECA_LOGGER::user_objects, "Cleaning child process pid=" + kvu_numtostr(pid_of_child()) + ".");
clean_child(true);
triggered_rep = false;
}
}
return pos;
}
void MP3FILE::set_parameter(int param, std::string value)
{
switch (param) {
case 1:
set_label(value);
break;
case 2:
long int numvalue = atol(value.c_str());
if (numvalue > 0)
bitrate_rep = numvalue;
else
bitrate_rep = MP3FILE::conf_default_output_bitrate;
break;
}
}
std::string MP3FILE::get_parameter(int param) const
{
switch (param) {
case 1:
return label();
case 2:
return kvu_numtostr(bitrate_rep);
}
return "";
}
void MP3FILE::get_mp3_params(const std::string& fname) throw(AUDIO_IO::SETUP_ERROR&)
{
std::string urlprefix;
struct frame fr;
if (mpg123_detect_by_content(fname.c_str(), &fr) != true) {
/* not a file, next search for an URL */
size_t offset = fname.find_first_of("://");
if (offset == std::string::npos) {
throw(SETUP_ERROR(SETUP_ERROR::io_mode, "AUDIOIO-MP3: Can't open " + label() + " for reading."));
}
else {
urlprefix = std::string(fname, 0, offset);
ECA_LOG_MSG(ECA_LOGGER::user_objects, "Found url; protocol '" + urlprefix + "'.");
}
}
else {
/* file size */
struct stat buf;
::stat(fname.c_str(), &buf);
double fsize = (double)buf.st_size;
ECA_LOG_MSG(ECA_LOGGER::user_objects, "Total file size (bytes): " + kvu_numtostr(fsize));
/* bitrate */
double bitrate = tabsel_123[fr.lsf][fr.lay - 1][fr.bitrate_index] * 1000;
ECA_LOG_MSG(ECA_LOGGER::user_objects, "Bitrate (bits/s): " + kvu_numtostr(bitrate));
/* sample freq */
double sfreq = mpg123_freqs[fr.sampling_frequency];
ECA_LOG_MSG(ECA_LOGGER::user_objects, "Sampling frequncy (Hz): " + kvu_numtostr(sfreq));
set_samples_per_second(static_cast<SAMPLE_SPECS::sample_rate_t>(sfreq));
/* channels */
// notice! mpg123 always outputs 16bit samples, stereo
mono_input_rep = (fr.mode == MPG_MD_MONO) ? true : false;
/* temporal length */
long int numframes = static_cast<long int>((fsize / mpg123_compute_bpf(&fr)));
ECA_LOG_MSG(ECA_LOGGER::user_objects, "Total length (frames): " + kvu_numtostr(numframes));
double tpf = mpg123_compute_tpf(&fr);
set_length_in_seconds(tpf * numframes);
ECA_LOG_MSG(ECA_LOGGER::user_objects, "Total length (seconds): " + kvu_numtostr(length_in_seconds()));
/* set pcm per frame value */
static int bs[4] = {0, 384, 1152, 1152};
pcm_rep = bs[fr.lay];
ECA_LOG_MSG(ECA_LOGGER::user_objects, "Pcm per mp3 frames: " + kvu_numtostr(pcm_rep));
}
/* sample format (this comes from mpg123) */
set_channels(2);
set_sample_format(ECA_AUDIO_FORMAT::sfmt_s16_le);
}
void MP3FILE::start_io(void)
{
if (triggered_rep != true) {
if (io_mode() == io_read)
fork_input_process();
else
fork_output_process();
triggered_rep = true;
}
}
void MP3FILE::stop_io(void)
{
if (triggered_rep == true) {
/* note: it's safe to send a SIGTERM if the client is
* an input and we know its PID (otherwise
* cleanup will still work but will take more time, which
* is nasty if we are in a middle of a seek */
if (io_mode() == io_read)
clean_child(true);
else
clean_child(false);
triggered_rep = false;
}
}
void MP3FILE::fork_input_process(void)
{
std::string cmd = MP3FILE::conf_input_cmd;
if (cmd.find("%o") != std::string::npos) {
cmd.replace(cmd.find("%o"), 2, kvu_numtostr((long)(position_in_samples() / pcm_rep)));
}
last_position_rep = position_in_samples();
ECA_LOG_MSG(ECA_LOGGER::user_objects, "" + cmd);
set_fork_command(cmd);
set_fork_file_name(label());
set_fork_bits(bits());
set_fork_channels(channels());
set_fork_sample_rate(samples_per_second()); /* for old mpg123 */
fork_child_for_read();
if (child_fork_succeeded() == true) {
/* NOTE: the file description will be closed by
* AUDIO_IO_FORKED_STREAM::clean_child() */
filedes_rep = file_descriptor();
filehandle_rep = fdopen(filedes_rep, "r"); /* not part of <cstdio> */
if (filehandle_rep == 0) {
finished_rep = true;
triggered_rep = false;
}
}
}
void MP3FILE::fork_output_process(void)
{
ECA_LOG_MSG(ECA_LOGGER::info, "Starting to encode " + label() + " with lame.");
last_position_rep = position_in_samples();
std::string cmd = MP3FILE::conf_output_cmd;
if (cmd.find("%B") != std::string::npos) {
cmd.replace(cmd.find("%B"), 2, kvu_numtostr((long int)(bitrate_rep / 1000)));
}
set_fork_command(cmd);
set_fork_file_name(label());
set_fork_bits(bits());
set_fork_channels(channels());
set_fork_sample_rate(samples_per_second());
fork_child_for_write();
if (child_fork_succeeded() == true) {
filedes_rep = file_descriptor();
}
}
|