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
|
/*****************************************************************
|
| AP4 - Track Objects
|
| Copyright 2002-2008 Axiomatic Systems, LLC
|
|
| This file is part of Bento4/AP4 (MP4 Atom Processing Library).
|
| Unless you have obtained Bento4 under a difference license,
| this version of Bento4 is Bento4|GPL.
| Bento4|GPL 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, or (at your option)
| any later version.
|
| Bento4|GPL 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 Bento4|GPL; see the file COPYING. If not, write to the
| Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
| 02111-1307, USA.
|
****************************************************************/
/*----------------------------------------------------------------------
| includes
+---------------------------------------------------------------------*/
#include "Ap4ByteStream.h"
#include "Ap4HdlrAtom.h"
#include "Ap4MvhdAtom.h"
#include "Ap4Track.h"
#include "Ap4Utils.h"
#include "Ap4Sample.h"
#include "Ap4DataBuffer.h"
#include "Ap4TrakAtom.h"
#include "Ap4TkhdAtom.h"
#include "Ap4MoovAtom.h"
#include "Ap4AtomSampleTable.h"
#include "Ap4SdpAtom.h"
#include "Ap4MdhdAtom.h"
#include "Ap4SyntheticSampleTable.h"
/*----------------------------------------------------------------------
| AP4_Track::AP4_Track
+---------------------------------------------------------------------*/
AP4_Track::AP4_Track(Type type,
AP4_SampleTable* sample_table,
AP4_UI32 track_id,
AP4_UI32 movie_time_scale,
AP4_UI64 track_duration,
AP4_UI32 media_time_scale,
AP4_UI64 media_duration,
const char* language,
AP4_UI32 width,
AP4_UI32 height) :
m_TrakAtomIsOwned(true),
m_Type(type),
m_SampleTable(sample_table),
m_SampleTableIsOwned(true),
m_MovieTimeScale(movie_time_scale ?
movie_time_scale :
AP4_TRACK_DEFAULT_MOVIE_TIMESCALE)
{
// compute the default volume value
unsigned int volume = 0;
if (type == TYPE_AUDIO) volume = 0x100;
// compute the handler type and name
AP4_Atom::Type hdlr_type;
const char* hdlr_name;
switch (type) {
case TYPE_AUDIO:
hdlr_type = AP4_HANDLER_TYPE_SOUN;
hdlr_name = "Bento4 Sound Handler";
break;
case TYPE_VIDEO:
hdlr_type = AP4_HANDLER_TYPE_VIDE;
hdlr_name = "Bento4 Video Handler";
break;
case TYPE_HINT:
hdlr_type = AP4_HANDLER_TYPE_HINT;
hdlr_name = "Bento4 Hint Handler";
break;
case TYPE_TEXT:
hdlr_type = AP4_HANDLER_TYPE_TEXT;
hdlr_name = "Bento4 Text Handler";
break;
case TYPE_SUBTITLES:
hdlr_type = AP4_HANDLER_TYPE_SUBT;
hdlr_name = "Bento4 Subtitle Handler";
break;
default:
hdlr_type = 0;
hdlr_name = NULL;
break;
}
// create a trak atom
m_TrakAtom = new AP4_TrakAtom(sample_table,
hdlr_type,
hdlr_name,
track_id,
0,
0,
track_duration,
media_time_scale,
media_duration,
(AP4_UI16)volume,
language,
width,
height);
}
/*----------------------------------------------------------------------
| AP4_Track::AP4_Track
+---------------------------------------------------------------------*/
AP4_Track::AP4_Track(AP4_SampleTable* sample_table,
AP4_UI32 track_id,
AP4_UI32 movie_time_scale, // 0 = use default
AP4_UI64 track_duration, // in the movie timescale
AP4_UI32 media_time_scale,
AP4_UI64 media_duration, // in the media timescale
const AP4_Track* track_prototype) :
m_TrakAtomIsOwned(true),
m_SampleTable(sample_table),
m_SampleTableIsOwned(true),
m_MovieTimeScale(movie_time_scale ?
movie_time_scale :
AP4_TRACK_DEFAULT_MOVIE_TIMESCALE)
{
// compute the handler type and name
AP4_Atom::Type hdlr_type;
const char* hdlr_name;
switch (track_prototype->GetType()) {
case TYPE_AUDIO:
hdlr_type = AP4_HANDLER_TYPE_SOUN;
hdlr_name = "Bento4 Sound Handler";
break;
case TYPE_VIDEO:
hdlr_type = AP4_HANDLER_TYPE_VIDE;
hdlr_name = "Bento4 Video Handler";
break;
case TYPE_HINT:
hdlr_type = AP4_HANDLER_TYPE_HINT;
hdlr_name = "Bento4 Hint Handler";
break;
case TYPE_TEXT:
hdlr_type = AP4_HANDLER_TYPE_TEXT;
hdlr_name = "Bento4 Text Handler";
break;
case TYPE_SUBTITLES:
hdlr_type = AP4_HANDLER_TYPE_SUBT;
hdlr_name = "Bento4 Subtitle Handler";
break;
default:
hdlr_type = track_prototype->GetHandlerType();
hdlr_name = track_prototype->GetTrackLanguage();
break;
}
// create a trak atom
const AP4_TkhdAtom* tkhd = NULL;
if (track_prototype->GetTrakAtom()) {
tkhd = track_prototype->GetTrakAtom()->GetTkhdAtom();
}
m_TrakAtom = new AP4_TrakAtom(sample_table,
hdlr_type,
hdlr_name,
track_id,
0,
0,
track_duration,
media_time_scale,
media_duration,
tkhd?tkhd->GetVolume():(track_prototype->GetType() == TYPE_AUDIO?0x100:0),
track_prototype->GetTrackLanguage(),
track_prototype->GetWidth(),
track_prototype->GetHeight(),
tkhd?tkhd->GetLayer():0,
tkhd?tkhd->GetAlternateGroup():0,
tkhd?tkhd->GetMatrix():NULL);
}
/*----------------------------------------------------------------------
| AP4_Track::AP4_Track
+---------------------------------------------------------------------*/
AP4_Track::AP4_Track(AP4_TrakAtom& atom,
AP4_ByteStream& sample_stream,
AP4_UI32 movie_time_scale) :
m_TrakAtom(&atom),
m_TrakAtomIsOwned(false),
m_Type(TYPE_UNKNOWN),
m_SampleTable(NULL),
m_SampleTableIsOwned(true),
m_MovieTimeScale(movie_time_scale)
{
// find the handler type
AP4_Atom* sub = atom.FindChild("mdia/hdlr");
if (sub) {
AP4_HdlrAtom* hdlr = AP4_DYNAMIC_CAST(AP4_HdlrAtom, sub);
if (hdlr) {
AP4_UI32 type = hdlr->GetHandlerType();
if (type == AP4_HANDLER_TYPE_SOUN) {
m_Type = TYPE_AUDIO;
} else if (type == AP4_HANDLER_TYPE_VIDE) {
m_Type = TYPE_VIDEO;
} else if (type == AP4_HANDLER_TYPE_HINT) {
m_Type = TYPE_HINT;
} else if (type == AP4_HANDLER_TYPE_ODSM ||
type == AP4_HANDLER_TYPE_SDSM) {
m_Type = TYPE_SYSTEM;
} else if (type == AP4_HANDLER_TYPE_TEXT ||
type == AP4_HANDLER_TYPE_TX3G) {
m_Type = TYPE_TEXT;
} else if (type == AP4_HANDLER_TYPE_JPEG) {
m_Type = TYPE_JPEG;
} else if (type == AP4_HANDLER_TYPE_SUBT ||
type == AP4_HANDLER_TYPE_SBTL) {
m_Type = TYPE_SUBTITLES;
}
}
}
// create a facade for the stbl atom
AP4_ContainerAtom* stbl = AP4_DYNAMIC_CAST(AP4_ContainerAtom, atom.FindChild("mdia/minf/stbl"));
if (stbl) {
m_SampleTable = new AP4_AtomSampleTable(stbl, sample_stream);
}
}
/*----------------------------------------------------------------------
| AP4_Track::~AP4_Track
+---------------------------------------------------------------------*/
AP4_Track::~AP4_Track()
{
if (m_TrakAtomIsOwned) delete m_TrakAtom;
if (m_SampleTableIsOwned) delete m_SampleTable;
}
/*----------------------------------------------------------------------
| AP4_Track::Clone
+---------------------------------------------------------------------*/
AP4_Track*
AP4_Track::Clone(AP4_Result* result)
{
AP4_SyntheticSampleTable* sample_table = new AP4_SyntheticSampleTable();
// default return value
if (result) *result = AP4_SUCCESS;
// add clones of the sample descriptions to the new sample table
for (unsigned int i=0; ;i++) {
AP4_SampleDescription* sample_description = GetSampleDescription(i);
if (sample_description == NULL) break;
sample_table->AddSampleDescription(sample_description->Clone());
}
AP4_Sample sample;
AP4_Ordinal index = 0;
while (AP4_SUCCEEDED(GetSample(index, sample))) {
AP4_ByteStream* data_stream;
data_stream = sample.GetDataStream();
sample_table->AddSample(*data_stream,
sample.GetOffset(),
sample.GetSize(),
sample.GetDuration(),
sample.GetDescriptionIndex(),
sample.GetDts(),
sample.GetCtsDelta(),
sample.IsSync());
AP4_RELEASE(data_stream); // release our ref, the table has kept its own ref.
index++;
}
// create the cloned track
AP4_Track* clone = new AP4_Track(sample_table,
GetId(),
GetMovieTimeScale(),
GetDuration(),
GetMediaTimeScale(),
GetMediaDuration(),
this);
return clone;
}
/*----------------------------------------------------------------------
| AP4_Track::Attach
+---------------------------------------------------------------------*/
AP4_Result
AP4_Track::Attach(AP4_MoovAtom* moov)
{
if (!m_TrakAtomIsOwned) return AP4_ERROR_INTERNAL;
moov->AddChild(m_TrakAtom);
m_TrakAtomIsOwned = false;
return AP4_SUCCESS;
}
/*----------------------------------------------------------------------
| AP4_Track::GetFlags
+---------------------------------------------------------------------*/
AP4_UI32
AP4_Track::GetFlags() const
{
if (m_TrakAtom) {
AP4_TkhdAtom* tkhd = AP4_DYNAMIC_CAST(AP4_TkhdAtom, m_TrakAtom->FindChild("tkhd"));
if (tkhd) {
return tkhd->GetFlags();
}
}
return 0;
}
/*----------------------------------------------------------------------
| AP4_Track::SetFlags
+---------------------------------------------------------------------*/
AP4_Result
AP4_Track::SetFlags(AP4_UI32 flags)
{
if (m_TrakAtom) {
AP4_TkhdAtom* tkhd = AP4_DYNAMIC_CAST(AP4_TkhdAtom, m_TrakAtom->FindChild("tkhd"));
if (tkhd) {
tkhd->SetFlags(flags);
return AP4_SUCCESS;
}
}
return AP4_ERROR_INVALID_STATE;
}
/*----------------------------------------------------------------------
| AP4_Track::GetHandlerType
+---------------------------------------------------------------------*/
AP4_UI32
AP4_Track::GetHandlerType() const
{
if (m_TrakAtom) {
AP4_HdlrAtom* hdlr = AP4_DYNAMIC_CAST(AP4_HdlrAtom, m_TrakAtom->FindChild("mdia/hdlr"));
if (hdlr) {
return hdlr->GetHandlerType();
}
}
return 0;
}
/*----------------------------------------------------------------------
| AP4_Track::GetId
+---------------------------------------------------------------------*/
AP4_UI32
AP4_Track::GetId() const
{
return m_TrakAtom->GetId();
}
/*----------------------------------------------------------------------
| AP4_Track::GetWidth
+---------------------------------------------------------------------*/
AP4_UI32
AP4_Track::GetWidth() const
{
return m_TrakAtom->GetWidth();
}
/*----------------------------------------------------------------------
| AP4_Track::GetHeight
+---------------------------------------------------------------------*/
AP4_UI32
AP4_Track::GetHeight() const
{
return m_TrakAtom->GetHeight();
}
/*----------------------------------------------------------------------
| AP4_Track::SetId
+---------------------------------------------------------------------*/
AP4_Result
AP4_Track::SetId(AP4_UI32 id)
{
m_TrakAtom->SetId(id);
return AP4_SUCCESS;
}
/*----------------------------------------------------------------------
| AP4_Track::GetDuration
+---------------------------------------------------------------------*/
AP4_UI64
AP4_Track::GetDuration() const
{
return m_TrakAtom->GetDuration();
}
/*----------------------------------------------------------------------
| AP4_Track::GetDurationMs
+---------------------------------------------------------------------*/
AP4_UI32
AP4_Track::GetDurationMs() const
{
AP4_UI64 duration = m_TrakAtom->GetDuration();
return AP4_DurationMsFromUnits(duration, m_MovieTimeScale);
}
/*----------------------------------------------------------------------
| AP4_Track::GetSampleCount
+---------------------------------------------------------------------*/
AP4_Cardinal
AP4_Track::GetSampleCount() const
{
// delegate to the sample table
return m_SampleTable ? m_SampleTable->GetSampleCount() : 0;
}
/*----------------------------------------------------------------------
| AP4_Track::GetSample
+---------------------------------------------------------------------*/
AP4_Result
AP4_Track::GetSample(AP4_Ordinal index, AP4_Sample& sample)
{
// delegate to the sample table
return m_SampleTable ? m_SampleTable->GetSample(index, sample) : AP4_FAILURE;
}
/*----------------------------------------------------------------------
| AP4_Track::GetSampleDescription
+---------------------------------------------------------------------*/
AP4_SampleDescription*
AP4_Track::GetSampleDescription(AP4_Ordinal index)
{
// delegate to the sample table
return m_SampleTable ? m_SampleTable->GetSampleDescription(index) : NULL;
}
/*----------------------------------------------------------------------
| AP4_Track::GetSampleDescriptionCount
+---------------------------------------------------------------------*/
AP4_Cardinal
AP4_Track::GetSampleDescriptionCount()
{
// delegate to the sample table
return m_SampleTable ? m_SampleTable->GetSampleDescriptionCount() : 0;
}
/*----------------------------------------------------------------------
| AP4_Track::ReadSample
+---------------------------------------------------------------------*/
AP4_Result
AP4_Track::ReadSample(AP4_Ordinal index,
AP4_Sample& sample,
AP4_DataBuffer& data)
{
AP4_Result result;
// get the sample
result = GetSample(index, sample);
if (AP4_FAILED(result)) return result;
// read the data
return sample.ReadData(data);
}
/*----------------------------------------------------------------------
| AP4_Track::GetSampleIndexForTimeStampMs
+---------------------------------------------------------------------*/
AP4_Result
AP4_Track::GetSampleIndexForTimeStampMs(AP4_UI32 ts_ms, AP4_Ordinal& index)
{
// convert the ts in the timescale of the track's media
AP4_UI64 ts = AP4_ConvertTime(ts_ms, 1000, GetMediaTimeScale());
return m_SampleTable->GetSampleIndexForTimeStamp(ts, index);
}
/*----------------------------------------------------------------------
| AP4_Track::GetNearestSyncSampleIndex
+---------------------------------------------------------------------*/
AP4_Ordinal
AP4_Track::GetNearestSyncSampleIndex(AP4_Ordinal index, bool before /* = true */)
{
if (m_SampleTable == NULL) return index;
return m_SampleTable->GetNearestSyncSampleIndex(index, before);
}
/*----------------------------------------------------------------------
| AP4_Track::SetMovieTimeScale
+---------------------------------------------------------------------*/
AP4_Result
AP4_Track::SetMovieTimeScale(AP4_UI32 time_scale)
{
// check that we can convert
if (m_MovieTimeScale == 0) return AP4_FAILURE;
// convert from one time scale to the other
m_TrakAtom->SetDuration(AP4_ConvertTime(m_TrakAtom->GetDuration(),
m_MovieTimeScale,
time_scale));
// keep the new movie timescale
m_MovieTimeScale = time_scale;
return AP4_SUCCESS;
}
/*----------------------------------------------------------------------
| AP4_Track::GetMediaTimeScale
+---------------------------------------------------------------------*/
AP4_UI32
AP4_Track::GetMediaTimeScale() const
{
return m_TrakAtom?m_TrakAtom->GetMediaTimeScale():0;
}
/*----------------------------------------------------------------------
| AP4_Track::GetMediaDuration
+---------------------------------------------------------------------*/
AP4_UI64
AP4_Track::GetMediaDuration() const
{
return m_TrakAtom?m_TrakAtom->GetMediaDuration():0;
}
/*----------------------------------------------------------------------
| AP4_Track::GetTrackName
+---------------------------------------------------------------------*/
const char*
AP4_Track::GetTrackName() const
{
if (AP4_HdlrAtom* hdlr = AP4_DYNAMIC_CAST(AP4_HdlrAtom, m_TrakAtom->FindChild("mdia/hdlr"))) {
return hdlr->GetHandlerName().GetChars();
}
return NULL;
}
/*----------------------------------------------------------------------
| AP4_Track::GetTrackLanguage
+---------------------------------------------------------------------*/
const char*
AP4_Track::GetTrackLanguage() const
{
if (AP4_MdhdAtom* mdhd = AP4_DYNAMIC_CAST(AP4_MdhdAtom, m_TrakAtom->FindChild("mdia/mdhd"))) {
return mdhd->GetLanguage().GetChars();
}
return NULL;
}
|