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
|
/*! \file
* implementation functions for class qmp3
*/
using namespace std;
#include <iostream>
# include "qmp3.hh"
# include "qexception.hh"
# include "qmisc.hh"
#ifdef NLS
# include <locale.h>
# include <libintl.h>
# define _(s) gettext (s)
#else
# define _(s) (s)
#endif
u_int32_t qmp3::append (qmp3 &mp3, bool force) {
// compatible guarantees both files are scanned
if (!compatible(mp3,force))
throw qexception (__PRETTY_FUNCTION__,
getName() + string(": ") +
mp3.getName() + string(_(" is not compatible")));
if (hasTag) {
qfile::truncate(getStreamLength());
remap(qfile::getMap());
hasTag = false;
}
qfile::append(mp3.qfile::getMap(),mp3.getStreamLength());
remap(qfile::getMap());
if (force && mp3.isVbr())
vbr=true;
frames += mp3.getFrames();
msduration += mp3.getMsDuration();
return frames;
}
bool qmp3::compatible(qmp3 &mp3, bool force) {
if (!isScanned())
scan();
if (!mp3.isScanned())
mp3.scan();
if (force)
return true;
if (isVbr() && mp3.isVbr())
return true;
// i la resta de parmetres...? actualitzar a la plana man/texinfo del qmp3join
return getBitRate()==mp3.getBitRate();
}
u_int32_t qmp3::getMsDuration() {
return (1000ULL * getFrames() * getSamplesPerFrame()) / getSampleRate();
}
qmp3::qmp3(string name, u_int32_t flags)
: qfile (name,flags),
qmp3frameheader (qfile::getMap()),
qtag (qfile::getMap()+getSize()-qtag::LENGTH) {
hasTag = isValid();
// frames = scan(getStreamLength());
scanned = false;
vbr = isVbr();
frames = getStreamLength()/qmp3frameheader::getLength();
#ifdef QVERBOSE
cerr << "mp3 file " << *this << " successfully opened." << endl;
#endif
}
// a ugly chooped scan() to discover fastly vbr streams
bool qmp3::isVbr() {
if (scanned)
return vbr;
u_int32_t bufsize = getStreamLength();
qmp3frameheader *fh = new qmp3frameheader(*this);
// if the file is little, easier to do a normal scan
// otherwise, there should be room enough for (int i=5) frames...
if (bufsize<10*fh->getLength()) {
scan();
return vbr;
}
u_int32_t bitrate = fh->getBitRate();
bufsize -= fh->getLength();
for (int i=5; i>0; i--) {
fh->setNext();
if (fh->getBitRate()!=bitrate)
return true;
}
return false;
}
u_int32_t qmp3::scan (u_int32_t bufsize) {
if (!bufsize)
bufsize = getStreamLength();
frames = 0;
qmp3frameheader *fh = new qmp3frameheader(*this);
u_int32_t bitrate = fh->getBitRate();
msduration = fh->getMsDuration();
if (bufsize<fh->getLength())
throw qexception(__PRETTY_FUNCTION__,_("first frame incomplete"));
bufsize -= fh->getLength();
frames++;
// char lluna[]={"OD)|(C"};
while (bufsize>qmp3frameheader::HEADERSIZE) {
fh->setNext();
if (bufsize>=fh->getLength()) {
bufsize -= fh->getLength();
frames++;
if (fh->getBitRate()!=bitrate)
bitrate = 0;
msduration += fh->getMsDuration();
// cout << lluna[frames%6] << "\r" << flush;
}
else
throw qexception(__PRETTY_FUNCTION__,
uint2string(bufsize)+string(_(" bytes of garbage at the end")));
}
#ifdef QVERBOSE
cerr << getName() << ": " << uint2string(frames) << " frames" << endl;
#endif
if (bufsize)
throw qexception(__PRETTY_FUNCTION__,
uint2string(bufsize)+string(_(" bytes of garbage at the end")));
scanned = true;
vbr = !bitrate;
return frames;
}
void qmp3::print (ostream *os) {
u_int32_t msduration;
*os << getName() << ": mpeg " << getVersion()
<< " layer " << getLayer() << " "
<< getSampleRate() << "Hz ";
if (!vbr)
*os << getBitRate() << "kbps";
else
*os << "[vbr]";
msduration = getMsDuration();
*os << " " << msduration/60000 << ":";
os->width(2);
os->fill('0');
*os << (msduration/1000)%60 << '.';
os->width(3);
os->fill('0');
*os << msduration%1000 << " ";
if (hasTag)
this->qtag::print(os);
else
*os << _("[no tag]");
}
ostream &operator<<(ostream &os, qmp3 &u) {
u.print(&os);
return os;
}
/*
void qmp3::truncate (u_int32_t frames) {
if (frames>=getFrames())
return;
qmp3frameheader fh(qfile::getMap()+getOffset(frames));
qfile::truncate(getOffset(frames)+fh.getLength());
remap(qfile::getMap());
}
*/
u_int32_t qmp3::cut (qcuthandler &h) {
if ((!h.getDel()) && (h.getOutfile()==""))
return 0;
if (!scanned) {
frames = scan();
scanned = true;
}
//
// an schema with the cut specifications and what will be done
// B => begin point, from the beginning of the file
// E => end point, from the beginning of the file
// b => begin point, from the end of the file
// e => end point, from the end of the file
// s => size of the fragment
//
// 0 means not specified 1 means specified
//
// B b E e s
// 0 0 0 0 0 throw
// 0 0 0 0 1 0 .. s
// 0 0 0 1 0 0 .. e
// 0 0 0 1 1 e-s .. e
// 0 0 1 0 0 0 .. E
// 0 0 1 0 1 E-s .. E
// 0 0 1 1 0 throw
// 0 0 1 1 1 throw
// 0 1 0 0 0 b .. EOF
// 0 1 0 0 1 b .. b+s
// 0 1 0 1 0 b .. e
// 0 1 0 1 1 throw
// 0 1 1 0 0 b .. E
// 0 1 1 0 1 throw
// 0 1 1 1 0 throw
// 0 1 1 1 1 throw
// 1 0 0 0 0 B .. EOF
// 1 0 0 0 1 B .. B+s
// 1 0 0 1 0 B .. e
// 1 0 0 1 1 throw
// 1 0 1 0 0 B .. E
// 1 0 1 0 1 throw
// 1 0 1 1 0 throw
// 1 0 1 1 1 throw
// 1 1 0 0 0 throw
// 1 1 0 0 1 throw
// 1 1 0 1 0 throw
// 1 1 0 1 1 throw
// 1 1 1 0 0 throw
// 1 1 1 0 1 throw
// 1 1 1 1 0 throw
// 1 1 1 1 1 throw
// it is ugly, actually, but the table above should make things clear
u_int32_t mode=0;
if (h.getBegin().getFormat()!=qvf::UNDEFINED)
mode+=16;
if (h.getbegin().getFormat()!=qvf::UNDEFINED)
mode+=8;
if (h.getEnd().getFormat()!=qvf::UNDEFINED)
mode+=4;
if (h.getend().getFormat()!=qvf::UNDEFINED)
mode+=2;
if (h.getSize().getFormat()!=qvf::UNDEFINED)
mode+=1;
u_int32_t first_frame, last_frame;
#ifdef QVERBOSE
cerr << "cut mode: " << mode << endl;
#endif
// comprovaci de zeros: -s 3b => -s 0!!
switch (mode) {
case 0:
//cerr << "cut: you must specify where to cut" << endl;
return 0;
case 1: // 1 .. s (p)
first_frame = 1;
last_frame = getFrame(h.getSize());
break;
case 2: // 1 .. e (p)
first_frame = 1;
last_frame = getFrames()+1-getFrame(h.getend());
break;
case 3: // e-s .. e (p)
last_frame = getFrames()+1-getFrame(h.getend());
first_frame = last_frame-getFrame(h.getSize())+1;
break;
case 4: // 1 .. E (p)
first_frame = 1;
last_frame = getFrame(h.getEnd());
break;
case 5: // E-s .. E (p)
last_frame = getFrame(h.getEnd());
first_frame = last_frame-getFrame(h.getSize())+1;
break;
case 6: // E+e => throw (p)
case 7: // E+e+s => throw (p)
case 14: // b+E+e => throw (p)
case 15: // b+E+e+s => throw (p)
case 22: // B+E+e => throw (p)
case 23: // B+E+e+s => throw (p)
throw qexception(__PRETTY_FUNCTION__,
_("options -E and -e cannot be used together"));
case 8: // b .. EOF (p)
first_frame = getFrames()+1-getFrame(h.getbegin());
last_frame = getFrames();
break;
case 9: // b .. b+s (p)
first_frame = getFrames()+1-getFrame(h.getbegin());
last_frame = first_frame+getFrame(h.getSize())-1;
break;
case 10: // b .. e (p)
first_frame = getFrames()+1-getFrame(h.getbegin());
last_frame = getFrames()+1-getFrame(h.getend());
break;
case 11: // b+e+s
case 13: // b+E+s
case 19: // B+e+s
case 21: // B+E+s
throw qexception(__PRETTY_FUNCTION__,
_("options (-b or -B), (-e or -E) and -s cannot be used together"));
case 12: // b .. E (p)
first_frame = getFrames()+1-getFrame(h.getbegin());
last_frame = getFrame(h.getEnd());
break;
case 16: // B .. EOF (p)
first_frame = getFrame(h.getBegin());
last_frame = getFrames();
break;
case 17: // B .. B+s (p)
first_frame = getFrame(h.getBegin());
last_frame = first_frame+getFrame(h.getSize())-1;
break;
case 18: // B .. e (p)
first_frame = getFrame(h.getBegin());
last_frame = getFrames()+1-getFrame(h.getend());
break;
case 20: // B .. E
first_frame = getFrame(h.getBegin());
last_frame = getFrame(h.getEnd());
break;
case 24 ... 31: // B+b+...
throw qexception(__PRETTY_FUNCTION__,
_("options -B and -b cannot be used together"));
default: // this should never happen
cerr << "quelcom panic!" << endl;
return 0;
}
#ifdef QVERBOSE
cerr << "cut: " << last_frame-first_frame+1 << " frames(" << first_frame << ".." << last_frame << ") => ";
#endif
if (first_frame>last_frame)
throw qexception(__PRETTY_FUNCTION__,_("last frame > first frame"));
// especificar quines mostres sn
if (last_frame>getFrames())
last_frame = getFrames();
if (h.getOutfile()!="")
getMp3(h.getOutfile(),first_frame,last_frame);
if (h.getDel())
cut(first_frame,last_frame);
return frames;
}
u_int32_t qmp3::getStreamLength () {
return getSize() - (hasTag?qtag::LENGTH:0);
}
u_int32_t qmp3::getFrames() { return frames; }
u_int32_t qmp3::getFrame (qvf &vf) {
u_int32_t frame;
switch (vf.getFormat()) {
case qvf::BYTES: frame = ((long long)vf.getValue()*getFrames())/getStreamLength(); break;
case qvf::KBYTES: frame = (vf.getValue()*getFrames()*1024ULL)/getStreamLength(); break;
case qvf::MBYTES: frame = (vf.getValue()*getFrames()*1024*1024ULL)/getStreamLength(); break;
case qvf::MILLISECONDS: frame = (vf.getValue()*getFrames()*1ULL)/getMsDuration(); break;
case qvf::SECONDS: frame = (vf.getValue()*getFrames()*1000ULL)/getMsDuration(); break;
case qvf::MINUTES: frame = (vf.getValue()*getFrames()*1000*60ULL)/getMsDuration(); break;
case qvf::SPECIFIC: frame = vf.getValue(); break;
default:
throw qexception(__PRETTY_FUNCTION__,
string(_("format not recognized: ")));
// hi ha una funci molt maca format2char
}
#ifdef QVERBOSE
cerr << "getFrame: " << vf << " => " << frame << endl;
#endif
if (frame<1 || frame>getFrames())
throw qexception(__PRETTY_FUNCTION__,
string(_("frame out of range: "))+uint2string(frame));
return frame;
}
u_int32_t qmp3::cut (u_int32_t firstframe, u_int32_t lastframe) {
u_int32_t offset_first = getOffset(firstframe);
u_int32_t offset_last = getOffset(lastframe);
qmp3frameheader fh(qfile::getMap()+offset_last);
u_int32_t length_last = fh.getLength();
qfile::cut(offset_first,offset_last+length_last-1);
remap(qfile::getMap());
}
// provar donant l'@ de memria...
u_int32_t qmp3::getOffset(u_int32_t frame) {
if (frame<1 || frame>getFrames())
throw qexception(__PRETTY_FUNCTION__,
string(_("frame out of range: "))+uint2string(frame));
if (!isScanned())
scan();
if (frame==1)
return 0;
// let's look around there...
long long offset; // more than 32 bits are needed!!
caddr_t start, backward, forward;
offset = ((long long)frame-1)*getStreamLength()/getFrames();
start = (caddr_t)qfile::getMap()+offset;
forward = seek_header(start,getSize()-offset,getSignature());
backward = seek_header(start,offset,getSignature(),true);
if ((char*)forward-(char*)start<(char*)start-(char*)backward) {
// cerr << "forward way: skipped " << (char*)forward-(char*)start << " bytes" << endl;
start = forward;
}
else {
// cerr << "backward way: skipped " << (char*)start-(char*)backward << " bytes" << endl;
start = backward;
}
#ifdef QVERBOSE
cerr << "frame " << frame
<< " => offset " << (char*)start-(char*)qfile::getMap()
<< "(" << offset << ")" << endl;
#endif
return (u_int32_t)((char*)start-(char*)qfile::getMap());
}
qmp3& qmp3::getMp3(string name, u_int32_t startframe, u_int32_t endframe) {
qfile newmp3(name,NEW);
u_int32_t startoffset = getOffset(startframe);
qmp3frameheader fh(qfile::getMap()+getOffset(endframe));
u_int32_t endoffset = getOffset(endframe)+fh.getLength();
newmp3.append((char*)qfile::getMap()+startoffset,endoffset-startoffset);
// return ??
}
bool qmp3::isScanned() { return scanned; }
|