Mercurial > audlegacy
annotate Plugins/Input/aac/libmp4v2/mp4file.cpp @ 199:0a2ad94e8607 trunk
[svn] Synced with bmp-mp4. Build system is fragile, but should work now.
author | chainsaw |
---|---|
date | Wed, 16 Nov 2005 16:21:11 -0800 |
parents | fa848bd484d8 |
children | f2dc045d2327 |
rev | line source |
---|---|
61 | 1 /* |
2 * The contents of this file are subject to the Mozilla Public | |
3 * License Version 1.1 (the "License"); you may not use this file | |
4 * except in compliance with the License. You may obtain a copy of | |
5 * the License at http://www.mozilla.org/MPL/ | |
6 * | |
7 * Software distributed under the License is distributed on an "AS | |
8 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or | |
9 * implied. See the License for the specific language governing | |
10 * rights and limitations under the License. | |
11 * | |
12 * The Original Code is MPEG4IP. | |
13 * | |
14 * The Initial Developer of the Original Code is Cisco Systems Inc. | |
15 * Portions created by Cisco Systems Inc. are | |
199
0a2ad94e8607
[svn] Synced with bmp-mp4. Build system is fragile, but should work now.
chainsaw
parents:
61
diff
changeset
|
16 * Copyright (C) Cisco Systems Inc. 2001 - 2004. All Rights Reserved. |
61 | 17 * |
18 * 3GPP features implementation is based on 3GPP's TS26.234-v5.60, | |
19 * and was contributed by Ximpo Group Ltd. | |
20 * | |
21 * Portions created by Ximpo Group Ltd. are | |
22 * Copyright (C) Ximpo Group Ltd. 2003, 2004. All Rights Reserved. | |
23 * | |
24 * Contributor(s): | |
25 * Dave Mackie dmackie@cisco.com | |
26 * Alix Marchandise-Franquet alix@cisco.com | |
27 * Ximpo Group Ltd. mp4v2@ximpo.com | |
28 */ | |
29 | |
30 #include "mp4common.h" | |
31 | |
32 MP4File::MP4File(u_int32_t verbosity) | |
33 { | |
34 m_fileName = NULL; | |
35 m_pFile = NULL; | |
36 m_orgFileSize = 0; | |
37 m_fileSize = 0; | |
38 m_pRootAtom = NULL; | |
39 m_odTrackId = MP4_INVALID_TRACK_ID; | |
40 | |
41 m_verbosity = verbosity; | |
42 m_mode = 0; | |
43 m_createFlags = 0; | |
44 m_useIsma = false; | |
45 | |
46 m_pModificationProperty = NULL; | |
47 m_pTimeScaleProperty = NULL; | |
48 m_pDurationProperty = NULL; | |
49 | |
50 m_memoryBuffer = NULL; | |
51 m_memoryBufferSize = 0; | |
52 m_memoryBufferPosition = 0; | |
53 | |
54 m_numReadBits = 0; | |
55 m_bufReadBits = 0; | |
56 m_numWriteBits = 0; | |
57 m_bufWriteBits = 0; | |
58 m_editName = NULL; | |
59 } | |
60 | |
61 MP4File::~MP4File() | |
62 { | |
63 MP4Free(m_fileName); | |
64 delete m_pRootAtom; | |
65 for (u_int32_t i = 0; i < m_pTracks.Size(); i++) { | |
66 delete m_pTracks[i]; | |
67 } | |
68 MP4Free(m_memoryBuffer); // just in case | |
69 CHECK_AND_FREE(m_editName); | |
70 | |
71 } | |
72 | |
73 void MP4File::Read(const char* fileName) | |
74 { | |
75 m_fileName = MP4Stralloc(fileName); | |
76 m_mode = 'r'; | |
77 | |
78 Open("rb"); | |
79 | |
80 ReadFromFile(); | |
81 | |
82 CacheProperties(); | |
83 } | |
84 | |
85 void MP4File::Create(const char* fileName, u_int32_t flags, | |
86 int add_ftyp, int add_iods, | |
87 char* majorBrand, u_int32_t minorVersion, | |
88 char** supportedBrands, u_int32_t supportedBrandsCount) | |
89 { | |
90 m_fileName = MP4Stralloc(fileName); | |
91 m_mode = 'w'; | |
92 m_createFlags = flags; | |
93 | |
94 Open("wb+"); | |
95 | |
96 // generate a skeletal atom tree | |
97 m_pRootAtom = MP4Atom::CreateAtom(NULL); | |
98 m_pRootAtom->SetFile(this); | |
99 m_pRootAtom->Generate(); | |
100 | |
101 if (add_ftyp != 0) { | |
102 MakeFtypAtom(majorBrand, minorVersion, | |
103 supportedBrands, supportedBrandsCount); | |
104 } | |
105 | |
106 CacheProperties(); | |
107 | |
108 // create mdat, and insert it after ftyp, and before moov | |
109 InsertChildAtom(m_pRootAtom, "mdat", | |
110 add_ftyp != 0 ? 1 : 0); | |
111 | |
112 // start writing | |
113 m_pRootAtom->BeginWrite(); | |
114 } | |
115 | |
116 bool MP4File::Use64Bits (const char *atomName) | |
117 { | |
118 if (!strcmp(atomName, "mdat") || !strcmp(atomName, "stbl")) { | |
119 return (m_createFlags & MP4_CREATE_64BIT_DATA) == MP4_CREATE_64BIT_DATA; | |
120 } | |
121 | |
122 if (!strcmp(atomName, "mvhd") || | |
123 !strcmp(atomName, "tkhd") || | |
124 !strcmp(atomName, "mdhd")) { | |
125 return (m_createFlags & MP4_CREATE_64BIT_TIME) == MP4_CREATE_64BIT_TIME; | |
126 } | |
127 return false; | |
128 } | |
129 | |
130 | |
131 void MP4File::Modify(const char* fileName) | |
132 { | |
133 m_fileName = MP4Stralloc(fileName); | |
134 m_mode = 'r'; | |
135 | |
136 Open("rb+"); | |
137 ReadFromFile(); | |
138 | |
139 m_mode = 'w'; | |
140 | |
141 // find the moov atom | |
142 MP4Atom* pMoovAtom = m_pRootAtom->FindAtom("moov"); | |
143 u_int32_t numAtoms; | |
144 | |
145 if (pMoovAtom == NULL) { | |
146 // there isn't one, odd but we can still proceed | |
147 pMoovAtom = AddChildAtom(m_pRootAtom, "moov"); | |
148 } else { | |
149 numAtoms = m_pRootAtom->GetNumberOfChildAtoms(); | |
150 | |
151 // work backwards thru the top level atoms | |
152 int32_t i; | |
153 bool lastAtomIsMoov = true; | |
154 MP4Atom* pLastAtom = NULL; | |
155 | |
156 for (i = numAtoms - 1; i >= 0; i--) { | |
157 MP4Atom* pAtom = m_pRootAtom->GetChildAtom(i); | |
158 const char* type = pAtom->GetType(); | |
159 | |
160 // get rid of any trailing free or skips | |
161 if (!strcmp(type, "free") || !strcmp(type, "skip")) { | |
162 m_pRootAtom->DeleteChildAtom(pAtom); | |
163 continue; | |
164 } | |
165 | |
166 if (strcmp(type, "moov")) { | |
167 if (pLastAtom == NULL) { | |
168 pLastAtom = pAtom; | |
169 lastAtomIsMoov = false; | |
170 } | |
171 continue; | |
172 } | |
173 | |
174 // now at moov atom | |
175 | |
176 // multiple moov atoms?!? | |
177 if (pAtom != pMoovAtom) { | |
178 throw new MP4Error( | |
179 "Badly formed mp4 file, multiple moov atoms", | |
180 "MP4Modify"); | |
181 } | |
182 | |
183 if (lastAtomIsMoov) { | |
184 // position to start of moov atom, | |
185 // effectively truncating file | |
186 // prior to adding new mdat | |
187 SetPosition(pMoovAtom->GetStart()); | |
188 | |
189 } else { // last atom isn't moov | |
190 // need to place a free atom | |
191 MP4Atom* pFreeAtom = MP4Atom::CreateAtom("free"); | |
192 | |
193 // in existing position of the moov atom | |
194 m_pRootAtom->InsertChildAtom(pFreeAtom, i); | |
195 m_pRootAtom->DeleteChildAtom(pMoovAtom); | |
196 m_pRootAtom->AddChildAtom(pMoovAtom); | |
197 | |
198 // write free atom to disk | |
199 SetPosition(pMoovAtom->GetStart()); | |
200 pFreeAtom->SetSize(pMoovAtom->GetSize()); | |
201 pFreeAtom->Write(); | |
202 | |
203 // finally set our file position to the end of the last atom | |
204 SetPosition(pLastAtom->GetEnd()); | |
205 } | |
206 | |
207 break; | |
208 } | |
209 ASSERT(i != -1); | |
210 } | |
211 | |
212 CacheProperties(); // of moov atom | |
213 | |
214 numAtoms = m_pRootAtom->GetNumberOfChildAtoms(); | |
215 | |
216 // insert another mdat prior to moov atom (the last atom) | |
217 MP4Atom* pMdatAtom = InsertChildAtom(m_pRootAtom, "mdat", numAtoms - 1); | |
218 | |
219 // start writing new mdat | |
220 pMdatAtom->BeginWrite(); | |
221 } | |
222 | |
223 void MP4File::Optimize(const char* orgFileName, const char* newFileName) | |
224 { | |
225 m_fileName = MP4Stralloc(orgFileName); | |
226 m_mode = 'r'; | |
227 | |
228 // first load meta-info into memory | |
229 Open("rb"); | |
230 ReadFromFile(); | |
231 | |
232 CacheProperties(); // of moov atom | |
233 | |
234 // now switch over to writing the new file | |
235 MP4Free(m_fileName); | |
236 | |
237 // create a temporary file if necessary | |
238 if (newFileName == NULL) { | |
239 m_fileName = MP4Stralloc(TempFileName()); | |
240 } else { | |
241 m_fileName = MP4Stralloc(newFileName); | |
242 } | |
243 | |
244 FILE* pReadFile = m_pFile; | |
245 m_pFile = NULL; | |
246 m_mode = 'w'; | |
247 | |
248 Open("wb"); | |
249 | |
250 SetIntegerProperty("moov.mvhd.modificationTime", | |
251 MP4GetAbsTimestamp()); | |
252 | |
253 // writing meta info in the optimal order | |
254 ((MP4RootAtom*)m_pRootAtom)->BeginOptimalWrite(); | |
255 | |
256 // write data in optimal order | |
257 RewriteMdat(pReadFile, m_pFile); | |
258 | |
259 // finish writing | |
260 ((MP4RootAtom*)m_pRootAtom)->FinishOptimalWrite(); | |
261 | |
262 // cleanup | |
263 fclose(m_pFile); | |
264 m_pFile = NULL; | |
265 fclose(pReadFile); | |
266 | |
267 // move temporary file into place | |
268 if (newFileName == NULL) { | |
269 Rename(m_fileName, orgFileName); | |
270 } | |
271 } | |
272 | |
273 void MP4File::RewriteMdat(FILE* pReadFile, FILE* pWriteFile) | |
274 { | |
275 u_int32_t numTracks = m_pTracks.Size(); | |
276 | |
277 MP4ChunkId* chunkIds = new MP4ChunkId[numTracks]; | |
278 MP4ChunkId* maxChunkIds = new MP4ChunkId[numTracks]; | |
279 MP4Timestamp* nextChunkTimes = new MP4Timestamp[numTracks]; | |
280 | |
281 for (u_int32_t i = 0; i < numTracks; i++) { | |
282 chunkIds[i] = 1; | |
283 maxChunkIds[i] = m_pTracks[i]->GetNumberOfChunks(); | |
284 nextChunkTimes[i] = MP4_INVALID_TIMESTAMP; | |
285 } | |
286 | |
287 while (true) { | |
288 u_int32_t nextTrackIndex = (u_int32_t)-1; | |
289 MP4Timestamp nextTime = MP4_INVALID_TIMESTAMP; | |
290 | |
291 for (u_int32_t i = 0; i < numTracks; i++) { | |
292 if (chunkIds[i] > maxChunkIds[i]) { | |
293 continue; | |
294 } | |
295 | |
296 if (nextChunkTimes[i] == MP4_INVALID_TIMESTAMP) { | |
297 MP4Timestamp chunkTime = | |
298 m_pTracks[i]->GetChunkTime(chunkIds[i]); | |
299 | |
300 nextChunkTimes[i] = MP4ConvertTime(chunkTime, | |
301 m_pTracks[i]->GetTimeScale(), GetTimeScale()); | |
302 } | |
303 | |
304 // time is not earliest so far | |
305 if (nextChunkTimes[i] > nextTime) { | |
306 continue; | |
307 } | |
308 | |
309 // prefer hint tracks to media tracks if times are equal | |
310 if (nextChunkTimes[i] == nextTime | |
311 && strcmp(m_pTracks[i]->GetType(), MP4_HINT_TRACK_TYPE)) { | |
312 continue; | |
313 } | |
314 | |
315 // this is our current choice of tracks | |
316 nextTime = nextChunkTimes[i]; | |
317 nextTrackIndex = i; | |
318 } | |
319 | |
320 if (nextTrackIndex == (u_int32_t)-1) { | |
321 break; | |
322 } | |
323 | |
324 // point into original mp4 file for read chunk call | |
325 m_pFile = pReadFile; | |
326 m_mode = 'r'; | |
327 | |
328 u_int8_t* pChunk; | |
329 u_int32_t chunkSize; | |
330 | |
331 m_pTracks[nextTrackIndex]-> | |
332 ReadChunk(chunkIds[nextTrackIndex], &pChunk, &chunkSize); | |
333 | |
334 // point back at the new mp4 file for write chunk | |
335 m_pFile = pWriteFile; | |
336 m_mode = 'w'; | |
337 | |
338 m_pTracks[nextTrackIndex]-> | |
339 RewriteChunk(chunkIds[nextTrackIndex], pChunk, chunkSize); | |
340 | |
341 MP4Free(pChunk); | |
342 | |
343 chunkIds[nextTrackIndex]++; | |
344 nextChunkTimes[nextTrackIndex] = MP4_INVALID_TIMESTAMP; | |
345 } | |
346 | |
347 delete [] chunkIds; | |
348 delete [] maxChunkIds; | |
349 delete [] nextChunkTimes; | |
350 } | |
351 | |
352 void MP4File::Open(const char* fmode) | |
353 { | |
354 ASSERT(m_pFile == NULL); | |
355 | |
356 #ifdef O_LARGEFILE | |
357 // UGH! fopen doesn't open a file in 64-bit mode, period. | |
358 // So we need to use open() and then fdopen() | |
359 int fd; | |
360 int flags = O_LARGEFILE; | |
361 | |
362 if (strchr(fmode, '+')) { | |
363 flags |= O_CREAT | O_RDWR; | |
364 if (fmode[0] == 'w') { | |
365 flags |= O_TRUNC; | |
366 } | |
367 } else { | |
368 if (fmode[0] == 'w') { | |
369 flags |= O_CREAT | O_TRUNC | O_WRONLY; | |
370 } else { | |
371 flags |= O_RDONLY; | |
372 } | |
373 } | |
374 fd = open(m_fileName, flags, 0666); | |
375 | |
376 if (fd >= 0) { | |
377 m_pFile = fdopen(fd, fmode); | |
378 } | |
379 #else | |
380 m_pFile = fopen(m_fileName, fmode); | |
381 #endif | |
382 if (m_pFile == NULL) { | |
383 throw new MP4Error(errno, "failed", "MP4Open"); | |
384 } | |
385 | |
386 if (m_mode == 'r') { | |
387 struct stat s; | |
388 if (fstat(fileno(m_pFile), &s) < 0) { | |
389 throw new MP4Error(errno, "stat failed", "MP4Open"); | |
390 } | |
391 m_orgFileSize = m_fileSize = s.st_size; | |
392 } else { | |
393 m_orgFileSize = m_fileSize = 0; | |
394 } | |
395 } | |
396 | |
397 void MP4File::ReadFromFile() | |
398 { | |
399 // ensure we start at beginning of file | |
400 SetPosition(0); | |
401 | |
402 // create a new root atom | |
403 ASSERT(m_pRootAtom == NULL); | |
404 m_pRootAtom = MP4Atom::CreateAtom(NULL); | |
405 | |
406 u_int64_t fileSize = GetSize(); | |
407 | |
408 m_pRootAtom->SetFile(this); | |
409 m_pRootAtom->SetStart(0); | |
410 m_pRootAtom->SetSize(fileSize); | |
411 m_pRootAtom->SetEnd(fileSize); | |
412 | |
413 m_pRootAtom->Read(); | |
414 | |
415 // create MP4Track's for any tracks in the file | |
416 GenerateTracks(); | |
417 } | |
418 | |
419 void MP4File::GenerateTracks() | |
420 { | |
421 u_int32_t trackIndex = 0; | |
422 | |
423 while (true) { | |
424 char trackName[32]; | |
425 snprintf(trackName, sizeof(trackName), "moov.trak[%u]", trackIndex); | |
426 | |
427 // find next trak atom | |
428 MP4Atom* pTrakAtom = m_pRootAtom->FindAtom(trackName); | |
429 | |
430 // done, no more trak atoms | |
431 if (pTrakAtom == NULL) { | |
432 break; | |
433 } | |
434 | |
435 // find track id property | |
436 MP4Integer32Property* pTrackIdProperty = NULL; | |
437 pTrakAtom->FindProperty( | |
438 "trak.tkhd.trackId", | |
439 (MP4Property**)&pTrackIdProperty); | |
440 | |
441 // find track type property | |
442 MP4StringProperty* pTypeProperty = NULL; | |
443 pTrakAtom->FindProperty( | |
444 "trak.mdia.hdlr.handlerType", | |
445 (MP4Property**)&pTypeProperty); | |
446 | |
447 // ensure we have the basics properties | |
448 if (pTrackIdProperty && pTypeProperty) { | |
449 | |
450 m_trakIds.Add(pTrackIdProperty->GetValue()); | |
451 | |
452 MP4Track* pTrack = NULL; | |
453 try { | |
454 if (!strcmp(pTypeProperty->GetValue(), MP4_HINT_TRACK_TYPE)) { | |
455 pTrack = new MP4RtpHintTrack(this, pTrakAtom); | |
456 } else { | |
457 pTrack = new MP4Track(this, pTrakAtom); | |
458 } | |
459 m_pTracks.Add(pTrack); | |
460 } | |
461 catch (MP4Error* e) { | |
462 VERBOSE_ERROR(m_verbosity, e->Print()); | |
463 delete e; | |
464 } | |
465 | |
466 // remember when we encounter the OD track | |
467 if (pTrack && !strcmp(pTrack->GetType(), MP4_OD_TRACK_TYPE)) { | |
468 if (m_odTrackId == MP4_INVALID_TRACK_ID) { | |
469 m_odTrackId = pTrackIdProperty->GetValue(); | |
470 } else { | |
471 VERBOSE_READ(GetVerbosity(), | |
472 printf("Warning: multiple OD tracks present\n")); | |
473 } | |
474 } | |
475 } else { | |
476 m_trakIds.Add(0); | |
477 } | |
478 | |
479 trackIndex++; | |
480 } | |
481 } | |
482 | |
483 void MP4File::CacheProperties() | |
484 { | |
485 FindIntegerProperty("moov.mvhd.modificationTime", | |
486 (MP4Property**)&m_pModificationProperty); | |
487 | |
488 FindIntegerProperty("moov.mvhd.timeScale", | |
489 (MP4Property**)&m_pTimeScaleProperty); | |
490 | |
491 FindIntegerProperty("moov.mvhd.duration", | |
492 (MP4Property**)&m_pDurationProperty); | |
493 } | |
494 | |
495 void MP4File::BeginWrite() | |
496 { | |
497 m_pRootAtom->BeginWrite(); | |
498 } | |
499 | |
500 void MP4File::FinishWrite() | |
501 { | |
502 // for all tracks, flush chunking buffers | |
503 for (u_int32_t i = 0; i < m_pTracks.Size(); i++) { | |
504 ASSERT(m_pTracks[i]); | |
505 m_pTracks[i]->FinishWrite(); | |
506 } | |
507 | |
508 // ask root atom to write | |
509 m_pRootAtom->FinishWrite(); | |
510 | |
511 // check if file shrunk, e.g. we deleted a track | |
512 if (GetSize() < m_orgFileSize) { | |
513 // just use a free atom to mark unused space | |
514 // MP4Optimize() should be used to clean up this space | |
515 MP4Atom* pFreeAtom = MP4Atom::CreateAtom("free"); | |
516 ASSERT(pFreeAtom); | |
517 pFreeAtom->SetFile(this); | |
518 int64_t size = m_orgFileSize - (m_fileSize + 8); | |
519 if (size < 0) size = 0; | |
520 pFreeAtom->SetSize(size); | |
521 pFreeAtom->Write(); | |
522 delete pFreeAtom; | |
523 } | |
524 } | |
525 | |
526 MP4Duration MP4File::UpdateDuration(MP4Duration duration) | |
527 { | |
528 MP4Duration currentDuration = GetDuration(); | |
529 if (duration > currentDuration) { | |
530 SetDuration(duration); | |
531 return duration; | |
532 } | |
533 return currentDuration; | |
534 } | |
535 | |
536 void MP4File::Dump(FILE* pDumpFile, bool dumpImplicits) | |
537 { | |
538 if (pDumpFile == NULL) { | |
539 pDumpFile = stdout; | |
540 } | |
541 | |
542 fprintf(pDumpFile, "Dumping %s meta-information...\n", m_fileName); | |
543 m_pRootAtom->Dump(pDumpFile, 0, dumpImplicits); | |
544 } | |
545 | |
546 void MP4File::Close() | |
547 { | |
548 if (m_mode == 'w') { | |
549 SetIntegerProperty("moov.mvhd.modificationTime", | |
550 MP4GetAbsTimestamp()); | |
551 | |
552 FinishWrite(); | |
553 } | |
554 | |
555 fclose(m_pFile); | |
556 m_pFile = NULL; | |
557 } | |
558 | |
559 const char* MP4File::TempFileName() | |
560 { | |
561 // there are so many attempts in libc to get this right | |
562 // that for portablity reasons, it's best just to roll our own | |
563 #ifndef _WIN32 | |
564 u_int32_t i; | |
565 for (i = getpid(); i < 0xFFFFFFFF; i++) { | |
566 sprintf(m_tempFileName, "./tmp%u.mp4", i); | |
567 if (access(m_tempFileName, F_OK) != 0) { | |
568 break; | |
569 } | |
570 } | |
571 if (i == 0xFFFFFFFF) { | |
572 throw new MP4Error("can't create temporary file", "TempFileName"); | |
573 } | |
574 #else | |
575 GetTempFileName(".", // dir. for temp. files | |
576 "mp4", // temp. filename prefix | |
577 0, // create unique name | |
578 m_tempFileName); // buffer for name | |
579 #endif | |
580 | |
581 return m_tempFileName; | |
582 } | |
583 | |
584 void MP4File::Rename(const char* oldFileName, const char* newFileName) | |
585 { | |
586 int rc; | |
587 | |
588 #ifdef _WIN32 | |
589 rc = remove(newFileName); | |
590 if (rc == 0) { | |
591 rc = rename(oldFileName, newFileName); | |
592 } | |
593 #else | |
594 rc = rename(oldFileName, newFileName); | |
595 #endif | |
596 if (rc != 0) { | |
597 throw new MP4Error(errno, "can't overwrite existing file", "Rename"); | |
598 } | |
599 } | |
600 | |
601 void MP4File::ProtectWriteOperation(char* where) | |
602 { | |
603 if (m_mode == 'r') { | |
604 throw new MP4Error("operation not permitted in read mode", where); | |
605 } | |
606 } | |
607 | |
608 MP4Track* MP4File::GetTrack(MP4TrackId trackId) | |
609 { | |
610 return m_pTracks[FindTrackIndex(trackId)]; | |
611 } | |
612 | |
613 MP4Atom* MP4File::FindAtom(const char* name) | |
614 { | |
615 MP4Atom* pAtom = NULL; | |
616 if (!name || !strcmp(name, "")) { | |
617 pAtom = m_pRootAtom; | |
618 } else { | |
619 pAtom = m_pRootAtom->FindAtom(name); | |
620 } | |
621 return pAtom; | |
622 } | |
623 | |
624 MP4Atom* MP4File::AddChildAtom( | |
625 const char* parentName, | |
626 const char* childName) | |
627 { | |
628 return AddChildAtom(FindAtom(parentName), childName); | |
629 } | |
630 | |
631 MP4Atom* MP4File::AddChildAtom( | |
632 MP4Atom* pParentAtom, | |
633 const char* childName) | |
634 { | |
635 return InsertChildAtom(pParentAtom, childName, | |
636 pParentAtom->GetNumberOfChildAtoms()); | |
637 } | |
638 | |
639 MP4Atom* MP4File::InsertChildAtom( | |
640 const char* parentName, | |
641 const char* childName, | |
642 u_int32_t index) | |
643 { | |
644 return InsertChildAtom(FindAtom(parentName), childName, index); | |
645 } | |
646 | |
647 MP4Atom* MP4File::InsertChildAtom( | |
648 MP4Atom* pParentAtom, | |
649 const char* childName, | |
650 u_int32_t index) | |
651 { | |
652 MP4Atom* pChildAtom = MP4Atom::CreateAtom(childName); | |
653 | |
654 ASSERT(pParentAtom); | |
655 pParentAtom->InsertChildAtom(pChildAtom, index); | |
656 | |
657 pChildAtom->Generate(); | |
658 | |
659 return pChildAtom; | |
660 } | |
661 | |
662 MP4Atom* MP4File::AddDescendantAtoms( | |
663 const char* ancestorName, | |
664 const char* descendantNames) | |
665 { | |
666 return AddDescendantAtoms(FindAtom(ancestorName), descendantNames); | |
667 } | |
668 | |
669 MP4Atom* MP4File::AddDescendantAtoms( | |
670 MP4Atom* pAncestorAtom, const char* descendantNames) | |
671 { | |
672 ASSERT(pAncestorAtom); | |
673 | |
674 MP4Atom* pParentAtom = pAncestorAtom; | |
675 MP4Atom* pChildAtom = NULL; | |
676 | |
677 while (true) { | |
678 char* childName = MP4NameFirst(descendantNames); | |
679 | |
680 if (childName == NULL) { | |
681 break; | |
682 } | |
683 | |
684 descendantNames = MP4NameAfterFirst(descendantNames); | |
685 | |
686 pChildAtom = pParentAtom->FindChildAtom(childName); | |
687 | |
688 if (pChildAtom == NULL) { | |
689 pChildAtom = AddChildAtom(pParentAtom, childName); | |
690 } | |
691 | |
692 pParentAtom = pChildAtom; | |
693 | |
694 MP4Free(childName); | |
695 } | |
696 | |
697 return pChildAtom; | |
698 } | |
699 | |
700 bool MP4File::FindProperty(const char* name, | |
701 MP4Property** ppProperty, u_int32_t* pIndex) | |
702 { | |
703 if (pIndex) { | |
704 *pIndex = 0; // set the default answer for index | |
705 } | |
706 | |
707 return m_pRootAtom->FindProperty(name, ppProperty, pIndex); | |
708 } | |
709 | |
710 void MP4File::FindIntegerProperty(const char* name, | |
711 MP4Property** ppProperty, u_int32_t* pIndex) | |
712 { | |
713 if (!FindProperty(name, ppProperty, pIndex)) { | |
714 throw new MP4Error("no such property - %s", "MP4File::FindIntegerProperty", name); | |
715 } | |
716 | |
717 switch ((*ppProperty)->GetType()) { | |
718 case Integer8Property: | |
719 case Integer16Property: | |
720 case Integer24Property: | |
721 case Integer32Property: | |
722 case Integer64Property: | |
723 break; | |
724 default: | |
725 throw new MP4Error("type mismatch - property %s type %d", "MP4File::FindIntegerProperty", name, (*ppProperty)->GetType()); | |
726 } | |
727 } | |
728 | |
729 u_int64_t MP4File::GetIntegerProperty(const char* name) | |
730 { | |
731 MP4Property* pProperty; | |
732 u_int32_t index; | |
733 | |
734 FindIntegerProperty(name, &pProperty, &index); | |
735 | |
736 return ((MP4IntegerProperty*)pProperty)->GetValue(index); | |
737 } | |
738 | |
739 void MP4File::SetIntegerProperty(const char* name, u_int64_t value) | |
740 { | |
741 ProtectWriteOperation("SetIntegerProperty"); | |
742 | |
743 MP4Property* pProperty = NULL; | |
744 u_int32_t index = 0; | |
745 | |
746 FindIntegerProperty(name, &pProperty, &index); | |
747 | |
748 ((MP4IntegerProperty*)pProperty)->SetValue(value, index); | |
749 } | |
750 | |
751 void MP4File::FindFloatProperty(const char* name, | |
752 MP4Property** ppProperty, u_int32_t* pIndex) | |
753 { | |
754 if (!FindProperty(name, ppProperty, pIndex)) { | |
755 throw new MP4Error("no such property - %s", "MP4File::FindFloatProperty", name); | |
756 } | |
757 if ((*ppProperty)->GetType() != Float32Property) { | |
758 throw new MP4Error("type mismatch - property %s type %d", | |
759 "MP4File::FindFloatProperty", | |
760 name, | |
761 (*ppProperty)->GetType()); | |
762 } | |
763 } | |
764 | |
765 float MP4File::GetFloatProperty(const char* name) | |
766 { | |
767 MP4Property* pProperty; | |
768 u_int32_t index; | |
769 | |
770 FindFloatProperty(name, &pProperty, &index); | |
771 | |
772 return ((MP4Float32Property*)pProperty)->GetValue(index); | |
773 } | |
774 | |
775 void MP4File::SetFloatProperty(const char* name, float value) | |
776 { | |
777 ProtectWriteOperation("SetFloatProperty"); | |
778 | |
779 MP4Property* pProperty; | |
780 u_int32_t index; | |
781 | |
782 FindFloatProperty(name, &pProperty, &index); | |
783 | |
784 ((MP4Float32Property*)pProperty)->SetValue(value, index); | |
785 } | |
786 | |
787 void MP4File::FindStringProperty(const char* name, | |
788 MP4Property** ppProperty, u_int32_t* pIndex) | |
789 { | |
790 if (!FindProperty(name, ppProperty, pIndex)) { | |
791 throw new MP4Error("no such property - %s", "MP4File::FindStringProperty", name); | |
792 } | |
793 if ((*ppProperty)->GetType() != StringProperty) { | |
794 throw new MP4Error("type mismatch - property %s type %d", "MP4File::FindStringProperty", | |
795 name, (*ppProperty)->GetType()); | |
796 } | |
797 } | |
798 | |
799 const char* MP4File::GetStringProperty(const char* name) | |
800 { | |
801 MP4Property* pProperty; | |
802 u_int32_t index; | |
803 | |
804 FindStringProperty(name, &pProperty, &index); | |
805 | |
806 return ((MP4StringProperty*)pProperty)->GetValue(index); | |
807 } | |
808 | |
809 void MP4File::SetStringProperty(const char* name, const char* value) | |
810 { | |
811 ProtectWriteOperation("SetStringProperty"); | |
812 | |
813 MP4Property* pProperty; | |
814 u_int32_t index; | |
815 | |
816 FindStringProperty(name, &pProperty, &index); | |
817 | |
818 ((MP4StringProperty*)pProperty)->SetValue(value, index); | |
819 } | |
820 | |
821 void MP4File::FindBytesProperty(const char* name, | |
822 MP4Property** ppProperty, u_int32_t* pIndex) | |
823 { | |
824 if (!FindProperty(name, ppProperty, pIndex)) { | |
825 throw new MP4Error("no such property %s", "MP4File::FindBytesProperty", name); | |
826 } | |
827 if ((*ppProperty)->GetType() != BytesProperty) { | |
828 throw new MP4Error("type mismatch - property %s - type %d", "MP4File::FindBytesProperty", name, (*ppProperty)->GetType()); | |
829 } | |
830 } | |
831 | |
832 void MP4File::GetBytesProperty(const char* name, | |
833 u_int8_t** ppValue, u_int32_t* pValueSize) | |
834 { | |
835 MP4Property* pProperty; | |
836 u_int32_t index; | |
837 | |
838 FindBytesProperty(name, &pProperty, &index); | |
839 | |
840 ((MP4BytesProperty*)pProperty)->GetValue(ppValue, pValueSize, index); | |
841 } | |
842 | |
843 void MP4File::SetBytesProperty(const char* name, | |
844 const u_int8_t* pValue, u_int32_t valueSize) | |
845 { | |
846 ProtectWriteOperation("SetBytesProperty"); | |
847 | |
848 MP4Property* pProperty; | |
849 u_int32_t index; | |
850 | |
851 FindBytesProperty(name, &pProperty, &index); | |
852 | |
853 ((MP4BytesProperty*)pProperty)->SetValue(pValue, valueSize, index); | |
854 } | |
855 | |
856 | |
857 // track functions | |
858 | |
859 MP4TrackId MP4File::AddTrack(const char* type, u_int32_t timeScale) | |
860 { | |
861 ProtectWriteOperation("AddTrack"); | |
862 | |
863 // create and add new trak atom | |
864 MP4Atom* pTrakAtom = AddChildAtom("moov", "trak"); | |
865 | |
866 // allocate a new track id | |
867 MP4TrackId trackId = AllocTrackId(); | |
868 | |
869 m_trakIds.Add(trackId); | |
870 | |
871 // set track id | |
872 MP4Integer32Property* pInteger32Property = NULL; | |
873 pTrakAtom->FindProperty( | |
874 "trak.tkhd.trackId", (MP4Property**)&pInteger32Property); | |
875 ASSERT(pInteger32Property); | |
876 pInteger32Property->SetValue(trackId); | |
877 | |
878 // set track type | |
879 const char* normType = MP4NormalizeTrackType(type, m_verbosity); | |
880 | |
881 // sanity check for user defined types | |
882 if (strlen(normType) > 4) { | |
883 VERBOSE_WARNING(m_verbosity, | |
884 printf("AddTrack: type truncated to four characters\n")); | |
885 // StringProperty::SetValue() will do the actual truncation | |
886 } | |
887 | |
888 MP4StringProperty* pStringProperty = NULL; | |
889 pTrakAtom->FindProperty( | |
890 "trak.mdia.hdlr.handlerType", (MP4Property**)&pStringProperty); | |
891 ASSERT(pStringProperty); | |
892 pStringProperty->SetValue(normType); | |
893 | |
894 // set track time scale | |
895 pInteger32Property = NULL; | |
896 pTrakAtom->FindProperty( | |
897 "trak.mdia.mdhd.timeScale", (MP4Property**)&pInteger32Property); | |
898 ASSERT(pInteger32Property); | |
899 pInteger32Property->SetValue(timeScale ? timeScale : 1000); | |
900 | |
901 // now have enough to create MP4Track object | |
902 MP4Track* pTrack = NULL; | |
903 if (!strcmp(normType, MP4_HINT_TRACK_TYPE)) { | |
904 pTrack = new MP4RtpHintTrack(this, pTrakAtom); | |
905 } else { | |
906 pTrack = new MP4Track(this, pTrakAtom); | |
907 } | |
908 m_pTracks.Add(pTrack); | |
909 | |
910 // mark non-hint tracks as enabled | |
911 if (strcmp(normType, MP4_HINT_TRACK_TYPE)) { | |
912 SetTrackIntegerProperty(trackId, "tkhd.flags", 1); | |
913 } | |
914 | |
915 // mark track as contained in this file | |
916 // LATER will provide option for external data references | |
917 AddDataReference(trackId, NULL); | |
918 | |
919 return trackId; | |
920 } | |
921 | |
922 void MP4File::AddTrackToIod(MP4TrackId trackId) | |
923 { | |
924 MP4DescriptorProperty* pDescriptorProperty = NULL; | |
925 m_pRootAtom->FindProperty("moov.iods.esIds", | |
926 (MP4Property**)&pDescriptorProperty); | |
927 ASSERT(pDescriptorProperty); | |
928 | |
929 MP4Descriptor* pDescriptor = | |
930 pDescriptorProperty->AddDescriptor(MP4ESIDIncDescrTag); | |
931 ASSERT(pDescriptor); | |
932 | |
933 MP4Integer32Property* pIdProperty = NULL; | |
934 pDescriptor->FindProperty("id", | |
935 (MP4Property**)&pIdProperty); | |
936 ASSERT(pIdProperty); | |
937 | |
938 pIdProperty->SetValue(trackId); | |
939 } | |
940 | |
941 void MP4File::RemoveTrackFromIod(MP4TrackId trackId, bool shallHaveIods) | |
942 { | |
943 MP4DescriptorProperty* pDescriptorProperty = NULL; | |
944 m_pRootAtom->FindProperty("moov.iods.esIds", | |
945 (MP4Property**)&pDescriptorProperty); | |
946 | |
947 if (shallHaveIods) { | |
948 ASSERT(pDescriptorProperty); | |
949 } else { | |
950 if (!pDescriptorProperty) { | |
951 return; | |
952 } | |
953 } | |
954 | |
955 for (u_int32_t i = 0; i < pDescriptorProperty->GetCount(); i++) { | |
956 /* static */char name[32]; | |
957 snprintf(name, sizeof(name), "esIds[%u].id", i); | |
958 | |
959 MP4Integer32Property* pIdProperty = NULL; | |
960 pDescriptorProperty->FindProperty(name, | |
961 (MP4Property**)&pIdProperty); | |
962 ASSERT(pIdProperty); | |
963 | |
964 if (pIdProperty->GetValue() == trackId) { | |
965 pDescriptorProperty->DeleteDescriptor(i); | |
966 break; | |
967 } | |
968 } | |
969 } | |
970 | |
971 void MP4File::AddTrackToOd(MP4TrackId trackId) | |
972 { | |
973 if (!m_odTrackId) { | |
974 return; | |
975 } | |
976 | |
977 AddTrackReference(MakeTrackName(m_odTrackId, "tref.mpod"), trackId); | |
978 } | |
979 | |
980 void MP4File::RemoveTrackFromOd(MP4TrackId trackId) | |
981 { | |
982 if (!m_odTrackId) { | |
983 return; | |
984 } | |
985 | |
986 RemoveTrackReference(MakeTrackName(m_odTrackId, "tref.mpod"), trackId); | |
987 } | |
988 | |
989 void MP4File::GetTrackReferenceProperties(const char* trefName, | |
990 MP4Property** ppCountProperty, MP4Property** ppTrackIdProperty) | |
991 { | |
992 char propName[1024]; | |
993 | |
994 snprintf(propName, sizeof(propName), "%s.%s", trefName, "entryCount"); | |
995 m_pRootAtom->FindProperty(propName, ppCountProperty); | |
996 ASSERT(*ppCountProperty); | |
997 | |
998 snprintf(propName, sizeof(propName), "%s.%s", trefName, "entries.trackId"); | |
999 m_pRootAtom->FindProperty(propName, ppTrackIdProperty); | |
1000 ASSERT(*ppTrackIdProperty); | |
1001 } | |
1002 | |
1003 void MP4File::AddTrackReference(const char* trefName, MP4TrackId refTrackId) | |
1004 { | |
1005 MP4Integer32Property* pCountProperty = NULL; | |
1006 MP4Integer32Property* pTrackIdProperty = NULL; | |
1007 | |
1008 GetTrackReferenceProperties(trefName, | |
1009 (MP4Property**)&pCountProperty, | |
1010 (MP4Property**)&pTrackIdProperty); | |
1011 | |
1012 pTrackIdProperty->AddValue(refTrackId); | |
1013 pCountProperty->IncrementValue(); | |
1014 } | |
1015 | |
1016 u_int32_t MP4File::FindTrackReference(const char* trefName, | |
1017 MP4TrackId refTrackId) | |
1018 { | |
1019 MP4Integer32Property* pCountProperty = NULL; | |
1020 MP4Integer32Property* pTrackIdProperty = NULL; | |
1021 | |
1022 GetTrackReferenceProperties(trefName, | |
1023 (MP4Property**)&pCountProperty, | |
1024 (MP4Property**)&pTrackIdProperty); | |
1025 | |
1026 for (u_int32_t i = 0; i < pCountProperty->GetValue(); i++) { | |
1027 if (refTrackId == pTrackIdProperty->GetValue(i)) { | |
1028 return i + 1; // N.B. 1 not 0 based index | |
1029 } | |
1030 } | |
1031 return 0; | |
1032 } | |
1033 | |
1034 void MP4File::RemoveTrackReference(const char* trefName, MP4TrackId refTrackId) | |
1035 { | |
1036 MP4Integer32Property* pCountProperty = NULL; | |
1037 MP4Integer32Property* pTrackIdProperty = NULL; | |
1038 | |
1039 GetTrackReferenceProperties(trefName, | |
1040 (MP4Property**)&pCountProperty, | |
1041 (MP4Property**)&pTrackIdProperty); | |
1042 | |
1043 for (u_int32_t i = 0; i < pCountProperty->GetValue(); i++) { | |
1044 if (refTrackId == pTrackIdProperty->GetValue(i)) { | |
1045 pTrackIdProperty->DeleteValue(i); | |
1046 pCountProperty->IncrementValue(-1); | |
1047 } | |
1048 } | |
1049 } | |
1050 | |
1051 void MP4File::AddDataReference(MP4TrackId trackId, const char* url) | |
1052 { | |
1053 MP4Atom* pDrefAtom = | |
1054 FindAtom(MakeTrackName(trackId, "mdia.minf.dinf.dref")); | |
1055 ASSERT(pDrefAtom); | |
1056 | |
1057 MP4Integer32Property* pCountProperty = NULL; | |
1058 pDrefAtom->FindProperty("dref.entryCount", | |
1059 (MP4Property**)&pCountProperty); | |
1060 ASSERT(pCountProperty); | |
1061 pCountProperty->IncrementValue(); | |
1062 | |
1063 MP4Atom* pUrlAtom = AddChildAtom(pDrefAtom, "url "); | |
1064 | |
1065 if (url && url[0] != '\0') { | |
1066 pUrlAtom->SetFlags(pUrlAtom->GetFlags() & 0xFFFFFE); | |
1067 | |
1068 MP4StringProperty* pUrlProperty = NULL; | |
1069 pUrlAtom->FindProperty("url .location", | |
1070 (MP4Property**)&pUrlProperty); | |
1071 ASSERT(pUrlProperty); | |
1072 pUrlProperty->SetValue(url); | |
1073 } else { | |
1074 pUrlAtom->SetFlags(pUrlAtom->GetFlags() | 1); | |
1075 } | |
1076 } | |
1077 | |
1078 MP4TrackId MP4File::AddSystemsTrack(const char* type) | |
1079 { | |
1080 const char* normType = MP4NormalizeTrackType(type, m_verbosity); | |
1081 | |
1082 // TBD if user type, fix name to four chars, and warn | |
1083 | |
1084 MP4TrackId trackId = AddTrack(type, MP4_MSECS_TIME_SCALE); | |
1085 | |
1086 InsertChildAtom(MakeTrackName(trackId, "mdia.minf"), "nmhd", 0); | |
1087 | |
1088 AddChildAtom(MakeTrackName(trackId, "mdia.minf.stbl.stsd"), "mp4s"); | |
1089 | |
1090 // stsd is a unique beast in that it has a count of the number | |
1091 // of child atoms that needs to be incremented after we add the mp4s atom | |
1092 MP4Integer32Property* pStsdCountProperty; | |
1093 FindIntegerProperty( | |
1094 MakeTrackName(trackId, "mdia.minf.stbl.stsd.entryCount"), | |
1095 (MP4Property**)&pStsdCountProperty); | |
1096 pStsdCountProperty->IncrementValue(); | |
1097 | |
1098 SetTrackIntegerProperty(trackId, | |
1099 "mdia.minf.stbl.stsd.mp4s.esds.ESID", | |
1100 #if 0 | |
1101 // note - for a file, these values need to | |
1102 // be 0 - wmay - 04/16/2003 | |
1103 trackId | |
1104 #else | |
1105 0 | |
1106 #endif | |
1107 ); | |
1108 | |
1109 SetTrackIntegerProperty(trackId, | |
1110 "mdia.minf.stbl.stsd.mp4s.esds.decConfigDescr.objectTypeId", | |
1111 MP4SystemsV1ObjectType); | |
1112 | |
1113 SetTrackIntegerProperty(trackId, | |
1114 "mdia.minf.stbl.stsd.mp4s.esds.decConfigDescr.streamType", | |
1115 ConvertTrackTypeToStreamType(normType)); | |
1116 | |
1117 return trackId; | |
1118 } | |
1119 | |
1120 MP4TrackId MP4File::AddODTrack() | |
1121 { | |
1122 // until a demonstrated need emerges | |
1123 // we limit ourselves to one object description track | |
1124 if (m_odTrackId != MP4_INVALID_TRACK_ID) { | |
1125 throw new MP4Error("object description track already exists", | |
1126 "AddObjectDescriptionTrack"); | |
1127 } | |
1128 | |
1129 m_odTrackId = AddSystemsTrack(MP4_OD_TRACK_TYPE); | |
1130 | |
1131 AddTrackToIod(m_odTrackId); | |
1132 | |
1133 AddDescendantAtoms(MakeTrackName(m_odTrackId, NULL), "tref.mpod"); | |
1134 | |
1135 return m_odTrackId; | |
1136 } | |
1137 | |
1138 MP4TrackId MP4File::AddSceneTrack() | |
1139 { | |
1140 MP4TrackId trackId = AddSystemsTrack(MP4_SCENE_TRACK_TYPE); | |
1141 | |
1142 AddTrackToIod(trackId); | |
1143 AddTrackToOd(trackId); | |
1144 | |
1145 return trackId; | |
1146 } | |
1147 | |
1148 // NULL terminated list of brands which require the IODS atom | |
1149 char *brandsWithIods[] = { "mp42", | |
1150 "isom", | |
1151 NULL}; | |
1152 | |
1153 bool MP4File::ShallHaveIods() | |
1154 { | |
1155 u_int32_t compatibleBrandsCount; | |
1156 MP4StringProperty *pMajorBrandProperty; | |
1157 | |
1158 MP4Atom* ftypAtom = m_pRootAtom->FindAtom("ftyp"); | |
1159 if (ftypAtom == NULL) return false; | |
1160 | |
1161 // Check the major brand | |
1162 ftypAtom->FindProperty( | |
1163 "ftyp.majorBrand", | |
1164 (MP4Property**)&pMajorBrandProperty); | |
1165 ASSERT(pMajorBrandProperty); | |
1166 for(u_int32_t j = 0 ; brandsWithIods[j] != NULL ; j++) { | |
1167 if (!strcasecmp( ((MP4StringProperty*)pMajorBrandProperty)->GetValue(), | |
1168 brandsWithIods[j])) | |
1169 return true; | |
1170 } | |
1171 | |
1172 // Check the compatible brands | |
1173 MP4Integer32Property* pCompatibleBrandsCountProperty; | |
1174 ftypAtom->FindProperty( | |
1175 "ftyp.compatibleBrandsCount", | |
1176 (MP4Property**)&pCompatibleBrandsCountProperty); | |
1177 ASSERT(pCompatibleBrandsCountProperty); | |
1178 | |
1179 compatibleBrandsCount = pCompatibleBrandsCountProperty->GetValue(); | |
1180 | |
1181 MP4TableProperty* pCompatibleBrandsProperty; | |
1182 ftypAtom->FindProperty( | |
1183 "ftyp.compatibleBrands", | |
1184 (MP4Property**)&pCompatibleBrandsProperty); | |
1185 | |
1186 MP4StringProperty* pBrandProperty = (MP4StringProperty*)pCompatibleBrandsProperty->GetProperty(0); | |
1187 ASSERT(pBrandProperty); | |
1188 | |
1189 for(u_int32_t i = 0 ; i < compatibleBrandsCount ; i++) { | |
1190 for(u_int32_t j = 0 ; brandsWithIods[j] != NULL ; j++) { | |
1191 if (!strcasecmp(pBrandProperty->GetValue(i), brandsWithIods[j])) | |
1192 return true; | |
1193 } | |
1194 } | |
1195 | |
1196 return false; | |
1197 } | |
1198 | |
1199 void MP4File::SetAmrVendor( | |
1200 MP4TrackId trackId, | |
1201 u_int32_t vendor) | |
1202 { | |
1203 SetTrackIntegerProperty(trackId, | |
1204 "mdia.minf.stbl.stsd.*.damr.vendor", | |
1205 vendor); | |
1206 } | |
1207 | |
1208 void MP4File::SetAmrDecoderVersion( | |
1209 MP4TrackId trackId, | |
1210 u_int8_t decoderVersion) | |
1211 { | |
1212 | |
1213 SetTrackIntegerProperty(trackId, | |
1214 "mdia.minf.stbl.stsd.*.damr.decoderVersion", | |
1215 decoderVersion); | |
1216 } | |
1217 | |
1218 void MP4File::SetAmrModeSet( | |
1219 MP4TrackId trackId, | |
1220 u_int16_t modeSet) | |
1221 { | |
1222 SetTrackIntegerProperty(trackId, | |
1223 "mdia.minf.stbl.stsd.*.damr.modeSet", | |
1224 modeSet); | |
1225 } | |
1226 uint16_t MP4File::GetAmrModeSet(MP4TrackId trackId) | |
1227 { | |
1228 return GetTrackIntegerProperty(trackId, | |
1229 "mdia.minf.stbl.stsd.*.damr.modeSet"); | |
1230 } | |
1231 | |
1232 MP4TrackId MP4File::AddAmrAudioTrack( | |
1233 u_int32_t timeScale, | |
1234 u_int16_t modeSet, | |
1235 u_int8_t modeChangePeriod, | |
1236 u_int8_t framesPerSample, | |
1237 bool isAmrWB) | |
1238 { | |
1239 | |
1240 u_int32_t fixedSampleDuration = (timeScale * 20)/1000; // 20mSec/Sample | |
1241 | |
1242 MP4TrackId trackId = AddTrack(MP4_AUDIO_TRACK_TYPE, timeScale); | |
1243 | |
1244 AddTrackToOd(trackId); | |
1245 | |
1246 SetTrackFloatProperty(trackId, "tkhd.volume", 1.0); | |
1247 | |
1248 InsertChildAtom(MakeTrackName(trackId, "mdia.minf"), "smhd", 0); | |
1249 | |
1250 AddChildAtom(MakeTrackName(trackId, "mdia.minf.stbl.stsd"), isAmrWB ? "sawb" : "samr"); | |
1251 | |
1252 // stsd is a unique beast in that it has a count of the number | |
1253 // of child atoms that needs to be incremented after we add the mp4a atom | |
1254 MP4Integer32Property* pStsdCountProperty; | |
1255 FindIntegerProperty( | |
1256 MakeTrackName(trackId, "mdia.minf.stbl.stsd.entryCount"), | |
1257 (MP4Property**)&pStsdCountProperty); | |
1258 pStsdCountProperty->IncrementValue(); | |
1259 | |
1260 SetTrackIntegerProperty(trackId, | |
1261 "mdia.minf.stbl.stsd.*.timeScale", | |
1262 timeScale); | |
1263 | |
1264 SetTrackIntegerProperty(trackId, | |
1265 "mdia.minf.stbl.stsd.*.damr.modeSet", | |
1266 modeSet); | |
1267 | |
1268 SetTrackIntegerProperty(trackId, | |
1269 "mdia.minf.stbl.stsd.*.damr.modeChangePeriod", | |
1270 modeChangePeriod); | |
1271 | |
1272 SetTrackIntegerProperty(trackId, | |
1273 "mdia.minf.stbl.stsd.*.damr.framesPerSample", | |
1274 framesPerSample); | |
1275 | |
1276 | |
1277 m_pTracks[FindTrackIndex(trackId)]-> | |
1278 SetFixedSampleDuration(fixedSampleDuration); | |
1279 | |
1280 return trackId; | |
1281 } | |
1282 | |
1283 MP4TrackId MP4File::AddAudioTrack( | |
1284 u_int32_t timeScale, | |
1285 MP4Duration sampleDuration, | |
1286 u_int8_t audioType) | |
1287 { | |
1288 MP4TrackId trackId = AddTrack(MP4_AUDIO_TRACK_TYPE, timeScale); | |
1289 | |
1290 AddTrackToOd(trackId); | |
1291 | |
1292 SetTrackFloatProperty(trackId, "tkhd.volume", 1.0); | |
1293 | |
1294 InsertChildAtom(MakeTrackName(trackId, "mdia.minf"), "smhd", 0); | |
1295 | |
1296 AddChildAtom(MakeTrackName(trackId, "mdia.minf.stbl.stsd"), "mp4a"); | |
1297 | |
1298 // stsd is a unique beast in that it has a count of the number | |
1299 // of child atoms that needs to be incremented after we add the mp4a atom | |
1300 MP4Integer32Property* pStsdCountProperty; | |
1301 FindIntegerProperty( | |
1302 MakeTrackName(trackId, "mdia.minf.stbl.stsd.entryCount"), | |
1303 (MP4Property**)&pStsdCountProperty); | |
1304 pStsdCountProperty->IncrementValue(); | |
1305 | |
1306 SetTrackIntegerProperty(trackId, | |
1307 "mdia.minf.stbl.stsd.mp4a.timeScale", timeScale); | |
1308 | |
1309 SetTrackIntegerProperty(trackId, | |
1310 "mdia.minf.stbl.stsd.mp4a.esds.ESID", | |
1311 #if 0 | |
1312 // note - for a file, these values need to | |
1313 // be 0 - wmay - 04/16/2003 | |
1314 trackId | |
1315 #else | |
1316 0 | |
1317 #endif | |
1318 ); | |
1319 | |
1320 SetTrackIntegerProperty(trackId, | |
1321 "mdia.minf.stbl.stsd.mp4a.esds.decConfigDescr.objectTypeId", | |
1322 audioType); | |
1323 | |
1324 SetTrackIntegerProperty(trackId, | |
1325 "mdia.minf.stbl.stsd.mp4a.esds.decConfigDescr.streamType", | |
1326 MP4AudioStreamType); | |
1327 | |
1328 m_pTracks[FindTrackIndex(trackId)]-> | |
1329 SetFixedSampleDuration(sampleDuration); | |
1330 | |
1331 return trackId; | |
1332 } | |
1333 | |
1334 MP4TrackId MP4File::AddEncAudioTrack(u_int32_t timeScale, | |
1335 MP4Duration sampleDuration, | |
1336 u_int8_t audioType, | |
1337 u_int32_t scheme_type, | |
1338 u_int16_t scheme_version, | |
1339 u_int8_t key_ind_len, | |
1340 u_int8_t iv_len, | |
1341 bool selective_enc, | |
1342 char *kms_uri | |
1343 ) | |
1344 { | |
1345 u_int32_t original_fmt = 0; | |
1346 | |
1347 MP4TrackId trackId = AddTrack(MP4_AUDIO_TRACK_TYPE, timeScale); | |
1348 | |
1349 AddTrackToOd(trackId); | |
1350 | |
1351 SetTrackFloatProperty(trackId, "tkhd.volume", 1.0); | |
1352 | |
1353 InsertChildAtom(MakeTrackName(trackId, "mdia.minf"), "smhd", 0); | |
1354 | |
1355 AddChildAtom(MakeTrackName(trackId, "mdia.minf.stbl.stsd"), "enca"); | |
1356 | |
1357 // stsd is a unique beast in that it has a count of the number | |
1358 // of child atoms that needs to be incremented after we add the enca atom | |
1359 MP4Integer32Property* pStsdCountProperty; | |
1360 FindIntegerProperty(MakeTrackName(trackId, "mdia.minf.stbl.stsd.entryCount"), | |
1361 (MP4Property**)&pStsdCountProperty); | |
1362 pStsdCountProperty->IncrementValue(); | |
1363 | |
1364 | |
1365 /* set all the ismacryp-specific values */ | |
1366 // original format is mp4a | |
1367 original_fmt = ('m'<<24 | 'p'<<16 | '4'<<8 | 'a'); | |
1368 SetTrackIntegerProperty(trackId, | |
1369 "mdia.minf.stbl.stsd.enca.sinf.frma.data-format", | |
1370 original_fmt); | |
1371 | |
1372 SetTrackIntegerProperty(trackId, | |
1373 "mdia.minf.stbl.stsd.enca.sinf.schm.scheme_type", | |
1374 scheme_type); | |
1375 | |
1376 SetTrackIntegerProperty(trackId, | |
1377 "mdia.minf.stbl.stsd.enca.sinf.schm.scheme_version", | |
1378 scheme_version); | |
1379 | |
1380 SetTrackStringProperty(trackId, | |
1381 "mdia.minf.stbl.stsd.enca.sinf.schi.iKMS.kms_URI", | |
1382 kms_uri); | |
1383 if (kms_uri != NULL) { | |
1384 free(kms_uri); | |
1385 } | |
1386 | |
1387 SetTrackIntegerProperty(trackId, | |
1388 "mdia.minf.stbl.stsd.enca.sinf.schi.iSFM.selective-encryption", | |
1389 selective_enc); | |
1390 | |
1391 SetTrackIntegerProperty(trackId, | |
1392 "mdia.minf.stbl.stsd.enca.sinf.schi.iSFM.key-indicator-length", | |
1393 key_ind_len); | |
1394 | |
1395 SetTrackIntegerProperty(trackId, | |
1396 "mdia.minf.stbl.stsd.enca.sinf.schi.iSFM.IV-length", | |
1397 iv_len); | |
1398 /* end ismacryp */ | |
1399 | |
1400 SetTrackIntegerProperty(trackId, | |
1401 "mdia.minf.stbl.stsd.enca.timeScale", timeScale); | |
1402 | |
1403 SetTrackIntegerProperty(trackId, | |
1404 "mdia.minf.stbl.stsd.enca.esds.ESID", | |
1405 #if 0 | |
1406 // note - for a file, these values need to | |
1407 // be 0 - wmay - 04/16/2003 | |
1408 trackId | |
1409 #else | |
1410 0 | |
1411 #endif | |
1412 ); | |
1413 | |
1414 SetTrackIntegerProperty(trackId, | |
1415 "mdia.minf.stbl.stsd.enca.esds.decConfigDescr.objectTypeId", | |
1416 audioType); | |
1417 | |
1418 SetTrackIntegerProperty(trackId, | |
1419 "mdia.minf.stbl.stsd.enca.esds.decConfigDescr.streamType", | |
1420 MP4AudioStreamType); | |
1421 | |
1422 m_pTracks[FindTrackIndex(trackId)]-> | |
1423 SetFixedSampleDuration(sampleDuration); | |
1424 | |
1425 return trackId; | |
1426 } | |
1427 | |
199
0a2ad94e8607
[svn] Synced with bmp-mp4. Build system is fragile, but should work now.
chainsaw
parents:
61
diff
changeset
|
1428 |
61 | 1429 MP4TrackId MP4File::AddVideoTrackDefault( |
1430 u_int32_t timeScale, | |
1431 MP4Duration sampleDuration, | |
1432 u_int16_t width, | |
1433 u_int16_t height, | |
1434 const char *videoType) | |
1435 { | |
1436 MP4TrackId trackId = AddTrack(MP4_VIDEO_TRACK_TYPE, timeScale); | |
1437 | |
1438 AddTrackToOd(trackId); | |
1439 | |
1440 SetTrackFloatProperty(trackId, "tkhd.width", width); | |
1441 SetTrackFloatProperty(trackId, "tkhd.height", height); | |
1442 | |
1443 InsertChildAtom(MakeTrackName(trackId, "mdia.minf"), "vmhd", 0); | |
1444 | |
1445 AddChildAtom(MakeTrackName(trackId, "mdia.minf.stbl.stsd"), videoType); | |
1446 | |
1447 // stsd is a unique beast in that it has a count of the number | |
1448 // of child atoms that needs to be incremented after we add the mp4v atom | |
1449 MP4Integer32Property* pStsdCountProperty; | |
1450 FindIntegerProperty( | |
1451 MakeTrackName(trackId, "mdia.minf.stbl.stsd.entryCount"), | |
1452 (MP4Property**)&pStsdCountProperty); | |
1453 pStsdCountProperty->IncrementValue(); | |
1454 | |
1455 SetTrackIntegerProperty(trackId, | |
1456 "mdia.minf.stbl.stsz.sampleSize", sampleDuration); | |
1457 | |
1458 m_pTracks[FindTrackIndex(trackId)]-> | |
1459 SetFixedSampleDuration(sampleDuration); | |
1460 | |
1461 return trackId; | |
1462 } | |
1463 MP4TrackId MP4File::AddMP4VideoTrack( | |
1464 u_int32_t timeScale, | |
1465 MP4Duration sampleDuration, | |
1466 u_int16_t width, | |
1467 u_int16_t height, | |
1468 u_int8_t videoType) | |
1469 { | |
1470 MP4TrackId trackId = AddVideoTrackDefault(timeScale, | |
1471 sampleDuration, | |
1472 width, | |
1473 height, | |
1474 "mp4v"); | |
1475 | |
1476 SetTrackIntegerProperty(trackId, | |
1477 "mdia.minf.stbl.stsd.mp4v.width", width); | |
1478 SetTrackIntegerProperty(trackId, | |
1479 "mdia.minf.stbl.stsd.mp4v.height", height); | |
1480 | |
1481 SetTrackIntegerProperty(trackId, | |
1482 "mdia.minf.stbl.stsd.mp4v.esds.ESID", | |
1483 #if 0 | |
1484 // note - for a file, these values need to | |
1485 // be 0 - wmay - 04/16/2003 | |
1486 trackId | |
1487 #else | |
1488 0 | |
1489 #endif | |
1490 ); | |
1491 | |
1492 SetTrackIntegerProperty(trackId, | |
1493 "mdia.minf.stbl.stsd.mp4v.esds.decConfigDescr.objectTypeId", | |
1494 videoType); | |
1495 | |
1496 SetTrackIntegerProperty(trackId, | |
1497 "mdia.minf.stbl.stsd.mp4v.esds.decConfigDescr.streamType", | |
1498 MP4VisualStreamType); | |
1499 | |
1500 return trackId; | |
1501 } | |
1502 | |
1503 MP4TrackId MP4File::AddEncVideoTrack(u_int32_t timeScale, | |
1504 MP4Duration sampleDuration, | |
1505 u_int16_t width, | |
1506 u_int16_t height, | |
1507 u_int8_t videoType, | |
1508 u_int32_t scheme_type, | |
1509 u_int16_t scheme_version, | |
1510 u_int8_t key_ind_len, | |
1511 u_int8_t iv_len, | |
1512 bool selective_enc, | |
1513 char *kms_uri | |
1514 ) | |
1515 { | |
1516 u_int32_t original_fmt = 0; | |
1517 | |
1518 MP4TrackId trackId = AddVideoTrackDefault(timeScale, | |
1519 sampleDuration, | |
1520 width, | |
1521 height, | |
1522 "encv"); | |
1523 | |
1524 SetTrackIntegerProperty(trackId, | |
1525 "mdia.minf.stbl.stsd.encv.width", width); | |
1526 SetTrackIntegerProperty(trackId, | |
1527 "mdia.minf.stbl.stsd.encv.height", height); | |
1528 | |
1529 /* set all the ismacryp-specific values */ | |
1530 // original format is mp4v | |
1531 original_fmt = ATOMID("mp4v"); | |
1532 SetTrackIntegerProperty(trackId, | |
1533 "mdia.minf.stbl.stsd.encv.sinf.frma.data-format", | |
1534 original_fmt); | |
1535 | |
1536 SetTrackIntegerProperty(trackId, | |
1537 "mdia.minf.stbl.stsd.encv.sinf.schm.scheme_type", | |
1538 scheme_type); | |
1539 | |
1540 SetTrackIntegerProperty(trackId, | |
1541 "mdia.minf.stbl.stsd.encv.sinf.schm.scheme_version", | |
1542 scheme_version); | |
1543 | |
1544 SetTrackStringProperty(trackId, | |
1545 "mdia.minf.stbl.stsd.encv.sinf.schi.iKMS.kms_URI", | |
1546 kms_uri); | |
1547 if (kms_uri != NULL) { | |
1548 free(kms_uri); | |
1549 } | |
1550 | |
1551 SetTrackIntegerProperty(trackId, | |
1552 "mdia.minf.stbl.stsd.encv.sinf.schi.iSFM.selective-encryption", | |
1553 selective_enc); | |
1554 | |
1555 SetTrackIntegerProperty(trackId, | |
1556 "mdia.minf.stbl.stsd.encv.sinf.schi.iSFM.key-indicator-length", | |
1557 key_ind_len); | |
1558 | |
1559 SetTrackIntegerProperty(trackId, | |
1560 "mdia.minf.stbl.stsd.encv.sinf.schi.iSFM.IV-length", | |
1561 iv_len); | |
1562 /* end ismacryp */ | |
1563 | |
1564 | |
1565 SetTrackIntegerProperty(trackId, | |
1566 "mdia.minf.stbl.stsd.encv.esds.ESID", | |
1567 #if 0 | |
1568 // note - for a file, these values need to | |
1569 // be 0 - wmay - 04/16/2003 | |
1570 trackId | |
1571 #else | |
1572 0 | |
1573 #endif | |
1574 ); | |
1575 | |
1576 SetTrackIntegerProperty(trackId, | |
1577 "mdia.minf.stbl.stsd.encv.esds.decConfigDescr.objectTypeId", | |
1578 videoType); | |
1579 | |
1580 SetTrackIntegerProperty(trackId, | |
1581 "mdia.minf.stbl.stsd.encv.esds.decConfigDescr.streamType", | |
1582 MP4VisualStreamType); | |
1583 | |
1584 return trackId; | |
1585 } | |
1586 | |
1587 MP4TrackId MP4File::AddH264VideoTrack( | |
1588 u_int32_t timeScale, | |
1589 MP4Duration sampleDuration, | |
1590 u_int16_t width, | |
1591 u_int16_t height, | |
1592 uint8_t AVCProfileIndication, | |
1593 uint8_t profile_compat, | |
1594 uint8_t AVCLevelIndication, | |
1595 uint8_t sampleLenFieldSizeMinusOne) | |
1596 { | |
1597 MP4TrackId trackId = AddVideoTrackDefault(timeScale, | |
1598 sampleDuration, | |
1599 width, | |
1600 height, | |
1601 "avc1"); | |
1602 | |
1603 SetTrackIntegerProperty(trackId, | |
1604 "mdia.minf.stbl.stsd.avc1.width", width); | |
1605 SetTrackIntegerProperty(trackId, | |
1606 "mdia.minf.stbl.stsd.avc1.height", height); | |
1607 | |
1608 /* shouldn't need this | |
1609 AddChildAtom(MakeTrackName(trackId, | |
1610 "mdia.minf.stbl.stsd.avc1"), | |
1611 "avcC"); | |
1612 */ | |
1613 SetTrackIntegerProperty(trackId, | |
1614 "mdia.minf.stbl.stsd.avc1.avcC.AVCProfileIndication", | |
1615 AVCProfileIndication); | |
1616 SetTrackIntegerProperty(trackId, | |
1617 "mdia.minf.stbl.stsd.avc1.avcC.profile_compatibility", | |
1618 profile_compat); | |
1619 SetTrackIntegerProperty(trackId, | |
1620 "mdia.minf.stbl.stsd.avc1.avcC.AVCLevelIndication", | |
1621 AVCLevelIndication); | |
1622 SetTrackIntegerProperty(trackId, | |
1623 "mdia.minf.stbl.stsd.avc1.avcC.lengthSizeMinusOne", | |
1624 sampleLenFieldSizeMinusOne); | |
1625 | |
1626 | |
1627 return trackId; | |
1628 } | |
1629 | |
1630 bool MP4File::AddH264SequenceParameterSet (MP4TrackId trackId, | |
199
0a2ad94e8607
[svn] Synced with bmp-mp4. Build system is fragile, but should work now.
chainsaw
parents:
61
diff
changeset
|
1631 uint8_t *pSequence, |
61 | 1632 uint16_t sequenceLen) |
1633 { | |
1634 MP4Atom *avcCAtom = | |
1635 FindAtom(MakeTrackName(trackId, | |
1636 "mdia.minf.stbl.stsd.avc1.avcC")); | |
1637 MP4BitfieldProperty *pCount; | |
1638 MP4Integer16Property *pLength; | |
1639 MP4BytesProperty *pUnit; | |
1640 if ((avcCAtom->FindProperty("avcC.numOfSequenceParameterSets", | |
1641 (MP4Property **)&pCount) == false) || | |
1642 (avcCAtom->FindProperty("avcC.sequenceEntries.sequenceParameterSetLength", | |
1643 (MP4Property **)&pLength) == false) || | |
1644 (avcCAtom->FindProperty("avcC.sequenceEntries.sequenceParameterSetNALUnit", | |
1645 (MP4Property **)&pUnit) == false)) { | |
1646 VERBOSE_ERROR(m_verbosity, WARNING("Could not find avcC properties")); | |
1647 return false; | |
1648 } | |
1649 uint32_t count = pCount->GetValue(); | |
1650 | |
1651 if (count > 0) { | |
1652 // see if we already exist | |
1653 for (uint32_t index = 0; index < count; index++) { | |
1654 if (pLength->GetValue(index) == sequenceLen) { | |
1655 uint8_t *seq; | |
1656 uint32_t seqlen; | |
1657 pUnit->GetValue(&seq, &seqlen, index); | |
1658 if (memcmp(seq, pSequence, sequenceLen) == 0) { | |
1659 free(seq); | |
1660 return true; | |
1661 } | |
1662 free(seq); | |
1663 } | |
1664 } | |
1665 } | |
1666 pLength->AddValue(sequenceLen); | |
1667 pUnit->AddValue(pSequence, sequenceLen); | |
1668 pCount->IncrementValue(); | |
1669 | |
1670 return true; | |
1671 } | |
1672 bool MP4File::AddH264PictureParameterSet (MP4TrackId trackId, | |
199
0a2ad94e8607
[svn] Synced with bmp-mp4. Build system is fragile, but should work now.
chainsaw
parents:
61
diff
changeset
|
1673 uint8_t *pPict, |
0a2ad94e8607
[svn] Synced with bmp-mp4. Build system is fragile, but should work now.
chainsaw
parents:
61
diff
changeset
|
1674 uint16_t pictLen) |
61 | 1675 { |
1676 MP4Atom *avcCAtom = | |
1677 FindAtom(MakeTrackName(trackId, | |
1678 "mdia.minf.stbl.stsd.avc1.avcC")); | |
1679 MP4Integer8Property *pCount; | |
1680 MP4Integer16Property *pLength; | |
1681 MP4BytesProperty *pUnit; | |
1682 if ((avcCAtom->FindProperty("avcC.numOfPictureParameterSets", | |
1683 (MP4Property **)&pCount) == false) || | |
1684 (avcCAtom->FindProperty("avcC.pictureEntries.pictureParameterSetLength", | |
1685 (MP4Property **)&pLength) == false) || | |
1686 (avcCAtom->FindProperty("avcC.pictureEntries.pictureParameterSetNALUnit", | |
1687 (MP4Property **)&pUnit) == false)) { | |
1688 VERBOSE_ERROR(m_verbosity, | |
1689 WARNING("Could not find avcC picture table properties")); | |
1690 return false; | |
1691 } | |
1692 uint32_t count = pCount->GetValue(); | |
1693 | |
1694 if (count > 0) { | |
1695 // see if we already exist | |
1696 for (uint32_t index = 0; index < count; index++) { | |
1697 if (pLength->GetValue(index) == pictLen) { | |
1698 uint8_t *seq; | |
1699 uint32_t seqlen; | |
1700 pUnit->GetValue(&seq, &seqlen, index); | |
1701 if (memcmp(seq, pPict, pictLen) == 0) { | |
1702 VERBOSE_WRITE(m_verbosity, | |
1703 fprintf(stderr, "picture matches %d\n", index)); | |
1704 free(seq); | |
1705 return true; | |
1706 } | |
1707 free(seq); | |
1708 } | |
1709 } | |
1710 } | |
1711 pLength->AddValue(pictLen); | |
1712 pUnit->AddValue(pPict, pictLen); | |
1713 pCount->IncrementValue(); | |
1714 VERBOSE_WRITE(m_verbosity, | |
1715 fprintf(stderr, "new picture added %d\n", pCount->GetValue())); | |
1716 | |
1717 return true; | |
1718 } | |
1719 void MP4File::SetH263Vendor( | |
1720 MP4TrackId trackId, | |
1721 u_int32_t vendor) | |
1722 { | |
1723 SetTrackIntegerProperty(trackId, | |
1724 "mdia.minf.stbl.stsd.s263.d263.vendor", | |
1725 vendor); | |
1726 } | |
1727 | |
1728 void MP4File::SetH263DecoderVersion( | |
1729 MP4TrackId trackId, | |
1730 u_int8_t decoderVersion) | |
1731 { | |
1732 SetTrackIntegerProperty(trackId, | |
1733 "mdia.minf.stbl.stsd.s263.d263.decoderVersion", | |
1734 decoderVersion); | |
1735 } | |
1736 | |
1737 void MP4File::SetH263Bitrates( | |
1738 MP4TrackId trackId, | |
1739 u_int32_t avgBitrate, | |
1740 u_int32_t maxBitrate) | |
1741 { | |
1742 SetTrackIntegerProperty(trackId, | |
1743 "mdia.minf.stbl.stsd.s263.d263.bitr.avgBitrate", | |
1744 avgBitrate); | |
1745 | |
1746 SetTrackIntegerProperty(trackId, | |
1747 "mdia.minf.stbl.stsd.s263.d263.bitr.maxBitrate", | |
1748 maxBitrate); | |
1749 | |
1750 } | |
1751 | |
1752 MP4TrackId MP4File::AddH263VideoTrack( | |
1753 u_int32_t timeScale, | |
1754 MP4Duration sampleDuration, | |
1755 u_int16_t width, | |
1756 u_int16_t height, | |
1757 u_int8_t h263Level, | |
1758 u_int8_t h263Profile, | |
1759 u_int32_t avgBitrate, | |
1760 u_int32_t maxBitrate) | |
1761 | |
1762 { | |
1763 MP4TrackId trackId = AddVideoTrackDefault(timeScale, | |
1764 sampleDuration, | |
1765 width, | |
1766 height, | |
1767 "s263"); | |
1768 | |
1769 SetTrackIntegerProperty(trackId, | |
1770 "mdia.minf.stbl.stsd.s263.width", width); | |
1771 SetTrackIntegerProperty(trackId, | |
1772 "mdia.minf.stbl.stsd.s263.height", height); | |
1773 | |
1774 SetTrackIntegerProperty(trackId, | |
1775 "mdia.minf.stbl.stsd.s263.d263.h263Level", h263Level); | |
1776 | |
1777 SetTrackIntegerProperty(trackId, | |
1778 "mdia.minf.stbl.stsd.s263.d263.h263Profile", h263Profile); | |
1779 | |
1780 // Add the bitr atom | |
1781 AddChildAtom(MakeTrackName(trackId, "mdia.minf.stbl.stsd.s263.d263"), | |
1782 "bitr"); | |
1783 | |
1784 SetTrackIntegerProperty(trackId, | |
1785 "mdia.minf.stbl.stsd.s263.d263.bitr.avgBitrate", avgBitrate); | |
1786 | |
1787 SetTrackIntegerProperty(trackId, | |
1788 "mdia.minf.stbl.stsd.s263.d263.bitr.maxBitrate", maxBitrate); | |
1789 | |
1790 | |
1791 SetTrackIntegerProperty(trackId, | |
1792 "mdia.minf.stbl.stsz.sampleSize", sampleDuration); | |
1793 | |
1794 return trackId; | |
1795 | |
1796 } | |
1797 | |
1798 MP4TrackId MP4File::AddHintTrack(MP4TrackId refTrackId) | |
1799 { | |
1800 // validate reference track id | |
1801 FindTrackIndex(refTrackId); | |
1802 | |
1803 MP4TrackId trackId = | |
1804 AddTrack(MP4_HINT_TRACK_TYPE, GetTrackTimeScale(refTrackId)); | |
1805 | |
1806 InsertChildAtom(MakeTrackName(trackId, "mdia.minf"), "hmhd", 0); | |
1807 | |
1808 AddChildAtom(MakeTrackName(trackId, "mdia.minf.stbl.stsd"), "rtp "); | |
1809 | |
1810 // stsd is a unique beast in that it has a count of the number | |
1811 // of child atoms that needs to be incremented after we add the rtp atom | |
1812 MP4Integer32Property* pStsdCountProperty; | |
1813 FindIntegerProperty( | |
1814 MakeTrackName(trackId, "mdia.minf.stbl.stsd.entryCount"), | |
1815 (MP4Property**)&pStsdCountProperty); | |
1816 pStsdCountProperty->IncrementValue(); | |
1817 | |
1818 SetTrackIntegerProperty(trackId, | |
1819 "mdia.minf.stbl.stsd.rtp .tims.timeScale", | |
1820 GetTrackTimeScale(trackId)); | |
1821 | |
1822 AddDescendantAtoms(MakeTrackName(trackId, NULL), "tref.hint"); | |
1823 | |
1824 AddTrackReference(MakeTrackName(trackId, "tref.hint"), refTrackId); | |
1825 | |
1826 AddDescendantAtoms(MakeTrackName(trackId, NULL), "udta.hnti.sdp "); | |
1827 | |
1828 AddDescendantAtoms(MakeTrackName(trackId, NULL), "udta.hinf"); | |
1829 | |
1830 return trackId; | |
1831 } | |
1832 | |
1833 void MP4File::DeleteTrack(MP4TrackId trackId) | |
1834 { | |
1835 ProtectWriteOperation("MP4DeleteTrack"); | |
1836 | |
1837 u_int32_t trakIndex = FindTrakAtomIndex(trackId); | |
1838 u_int16_t trackIndex = FindTrackIndex(trackId); | |
1839 MP4Track* pTrack = m_pTracks[trackIndex]; | |
1840 | |
1841 MP4Atom* pTrakAtom = pTrack->GetTrakAtom(); | |
1842 ASSERT(pTrakAtom); | |
1843 | |
1844 MP4Atom* pMoovAtom = FindAtom("moov"); | |
1845 ASSERT(pMoovAtom); | |
1846 | |
1847 RemoveTrackFromIod(trackId, ShallHaveIods()); | |
1848 RemoveTrackFromOd(trackId); | |
1849 | |
1850 if (trackId == m_odTrackId) { | |
1851 m_odTrackId = 0; | |
1852 } | |
1853 | |
1854 pMoovAtom->DeleteChildAtom(pTrakAtom); | |
1855 | |
1856 m_trakIds.Delete(trakIndex); | |
1857 | |
1858 m_pTracks.Delete(trackIndex); | |
1859 | |
1860 delete pTrack; | |
1861 delete pTrakAtom; | |
1862 } | |
1863 | |
1864 u_int32_t MP4File::GetNumberOfTracks(const char* type, u_int8_t subType) | |
1865 { | |
1866 if (type == NULL) { | |
1867 return m_pTracks.Size(); | |
1868 } | |
1869 | |
1870 u_int32_t typeSeen = 0; | |
1871 const char* normType = MP4NormalizeTrackType(type, m_verbosity); | |
1872 | |
1873 for (u_int32_t i = 0; i < m_pTracks.Size(); i++) { | |
1874 if (!strcmp(normType, m_pTracks[i]->GetType())) { | |
1875 if (subType) { | |
1876 if (normType == MP4_AUDIO_TRACK_TYPE) { | |
1877 if (subType != GetTrackEsdsObjectTypeId(m_pTracks[i]->GetId())) { | |
1878 continue; | |
1879 } | |
1880 } else if (normType == MP4_VIDEO_TRACK_TYPE) { | |
1881 if (subType != GetTrackEsdsObjectTypeId(m_pTracks[i]->GetId())) { | |
1882 continue; | |
1883 } | |
1884 } | |
1885 // else unknown subtype, ignore it | |
1886 } | |
1887 typeSeen++; | |
1888 } | |
1889 } | |
1890 return typeSeen; | |
1891 } | |
1892 | |
1893 MP4TrackId MP4File::AllocTrackId() | |
1894 { | |
1895 MP4TrackId trackId = | |
1896 GetIntegerProperty("moov.mvhd.nextTrackId"); | |
1897 | |
1898 if (trackId <= 0xFFFF) { | |
1899 // check that nextTrackid is correct | |
1900 try { | |
1901 FindTrackIndex(trackId); | |
1902 // ERROR, this trackId is in use | |
1903 } | |
1904 catch (MP4Error* e) { | |
1905 // OK, this trackId is not in use, proceed | |
1906 delete e; | |
1907 SetIntegerProperty("moov.mvhd.nextTrackId", trackId + 1); | |
1908 return trackId; | |
1909 } | |
1910 } | |
1911 | |
1912 // we need to search for a track id | |
1913 for (trackId = 1; trackId <= 0xFFFF; trackId++) { | |
1914 try { | |
1915 FindTrackIndex(trackId); | |
1916 // KEEP LOOKING, this trackId is in use | |
1917 } | |
1918 catch (MP4Error* e) { | |
1919 // OK, this trackId is not in use, proceed | |
1920 delete e; | |
1921 return trackId; | |
1922 } | |
1923 } | |
1924 | |
1925 // extreme case where mp4 file has 2^16 tracks in it | |
1926 throw new MP4Error("too many existing tracks", "AddTrack"); | |
1927 return MP4_INVALID_TRACK_ID; // to keep MSVC happy | |
1928 } | |
1929 | |
1930 MP4TrackId MP4File::FindTrackId(u_int16_t trackIndex, | |
1931 const char* type, u_int8_t subType) | |
1932 { | |
1933 if (type == NULL) { | |
1934 return m_pTracks[trackIndex]->GetId(); | |
1935 } | |
1936 | |
1937 u_int32_t typeSeen = 0; | |
1938 const char* normType = MP4NormalizeTrackType(type, m_verbosity); | |
1939 | |
1940 for (u_int32_t i = 0; i < m_pTracks.Size(); i++) { | |
1941 if (!strcmp(normType, m_pTracks[i]->GetType())) { | |
1942 if (subType) { | |
1943 if (normType == MP4_AUDIO_TRACK_TYPE) { | |
1944 if (subType != GetTrackEsdsObjectTypeId(m_pTracks[i]->GetId())) { | |
1945 continue; | |
1946 } | |
1947 } else if (normType == MP4_VIDEO_TRACK_TYPE) { | |
1948 if (subType != GetTrackEsdsObjectTypeId(m_pTracks[i]->GetId())) { | |
1949 continue; | |
1950 } | |
1951 } | |
1952 // else unknown subtype, ignore it | |
1953 } | |
1954 | |
1955 if (trackIndex == typeSeen) { | |
1956 return m_pTracks[i]->GetId(); | |
1957 } | |
1958 | |
1959 typeSeen++; | |
1960 } | |
1961 } | |
1962 | |
1963 throw new MP4Error("Track index doesn't exist - track %d type %s", | |
1964 "FindTrackId", | |
1965 trackIndex, type); | |
1966 return MP4_INVALID_TRACK_ID; // satisfy MS compiler | |
1967 } | |
1968 | |
1969 u_int16_t MP4File::FindTrackIndex(MP4TrackId trackId) | |
1970 { | |
1971 for (u_int32_t i = 0; i < m_pTracks.Size() && i <= 0xFFFF; i++) { | |
1972 if (m_pTracks[i]->GetId() == trackId) { | |
1973 return (u_int16_t)i; | |
1974 } | |
1975 } | |
1976 | |
1977 throw new MP4Error("Track id %d doesn't exist", "FindTrackIndex", trackId); | |
1978 return (u_int16_t)-1; // satisfy MS compiler | |
1979 } | |
1980 | |
1981 u_int16_t MP4File::FindTrakAtomIndex(MP4TrackId trackId) | |
1982 { | |
1983 if (trackId) { | |
1984 for (u_int32_t i = 0; i < m_trakIds.Size(); i++) { | |
1985 if (m_trakIds[i] == trackId) { | |
1986 return i; | |
1987 } | |
1988 } | |
1989 } | |
1990 | |
1991 throw new MP4Error("Track id %d doesn't exist", "FindTrakAtomIndex", | |
1992 trackId); | |
1993 return (u_int16_t)-1; // satisfy MS compiler | |
1994 } | |
1995 | |
1996 u_int32_t MP4File::GetSampleSize(MP4TrackId trackId, MP4SampleId sampleId) | |
1997 { | |
1998 return m_pTracks[FindTrackIndex(trackId)]->GetSampleSize(sampleId); | |
1999 } | |
2000 | |
2001 u_int32_t MP4File::GetTrackMaxSampleSize(MP4TrackId trackId) | |
2002 { | |
2003 return m_pTracks[FindTrackIndex(trackId)]->GetMaxSampleSize(); | |
2004 } | |
2005 | |
2006 MP4SampleId MP4File::GetSampleIdFromTime(MP4TrackId trackId, | |
2007 MP4Timestamp when, bool wantSyncSample) | |
2008 { | |
2009 return m_pTracks[FindTrackIndex(trackId)]-> | |
2010 GetSampleIdFromTime(when, wantSyncSample); | |
2011 } | |
2012 | |
2013 MP4Timestamp MP4File::GetSampleTime( | |
2014 MP4TrackId trackId, MP4SampleId sampleId) | |
2015 { | |
2016 MP4Timestamp timestamp; | |
2017 m_pTracks[FindTrackIndex(trackId)]-> | |
2018 GetSampleTimes(sampleId, ×tamp, NULL); | |
2019 return timestamp; | |
2020 } | |
2021 | |
2022 MP4Duration MP4File::GetSampleDuration( | |
2023 MP4TrackId trackId, MP4SampleId sampleId) | |
2024 { | |
2025 MP4Duration duration; | |
2026 m_pTracks[FindTrackIndex(trackId)]-> | |
2027 GetSampleTimes(sampleId, NULL, &duration); | |
2028 return duration; | |
2029 } | |
2030 | |
2031 MP4Duration MP4File::GetSampleRenderingOffset( | |
2032 MP4TrackId trackId, MP4SampleId sampleId) | |
2033 { | |
2034 return m_pTracks[FindTrackIndex(trackId)]-> | |
2035 GetSampleRenderingOffset(sampleId); | |
2036 } | |
2037 | |
2038 bool MP4File::GetSampleSync(MP4TrackId trackId, MP4SampleId sampleId) | |
2039 { | |
2040 return m_pTracks[FindTrackIndex(trackId)]->IsSyncSample(sampleId); | |
2041 } | |
2042 | |
2043 void MP4File::ReadSample(MP4TrackId trackId, MP4SampleId sampleId, | |
2044 u_int8_t** ppBytes, u_int32_t* pNumBytes, | |
2045 MP4Timestamp* pStartTime, MP4Duration* pDuration, | |
2046 MP4Duration* pRenderingOffset, bool* pIsSyncSample) | |
2047 { | |
2048 m_pTracks[FindTrackIndex(trackId)]-> | |
2049 ReadSample(sampleId, ppBytes, pNumBytes, | |
2050 pStartTime, pDuration, pRenderingOffset, pIsSyncSample); | |
2051 } | |
2052 | |
2053 void MP4File::WriteSample(MP4TrackId trackId, | |
2054 const u_int8_t* pBytes, u_int32_t numBytes, | |
2055 MP4Duration duration, MP4Duration renderingOffset, bool isSyncSample) | |
2056 { | |
2057 ProtectWriteOperation("MP4WriteSample"); | |
2058 | |
2059 m_pTracks[FindTrackIndex(trackId)]-> | |
2060 WriteSample(pBytes, numBytes, duration, renderingOffset, isSyncSample); | |
2061 | |
2062 m_pModificationProperty->SetValue(MP4GetAbsTimestamp()); | |
2063 } | |
2064 | |
2065 void MP4File::SetSampleRenderingOffset(MP4TrackId trackId, | |
2066 MP4SampleId sampleId, MP4Duration renderingOffset) | |
2067 { | |
2068 ProtectWriteOperation("MP4SetSampleRenderingOffset"); | |
2069 | |
2070 m_pTracks[FindTrackIndex(trackId)]-> | |
2071 SetSampleRenderingOffset(sampleId, renderingOffset); | |
2072 | |
2073 m_pModificationProperty->SetValue(MP4GetAbsTimestamp()); | |
2074 } | |
2075 | |
2076 char* MP4File::MakeTrackName(MP4TrackId trackId, const char* name) | |
2077 { | |
2078 u_int16_t trakIndex = FindTrakAtomIndex(trackId); | |
2079 | |
2080 if (name == NULL || name[0] == '\0') { | |
2081 snprintf(m_trakName, sizeof(m_trakName), | |
2082 "moov.trak[%u]", trakIndex); | |
2083 } else { | |
2084 snprintf(m_trakName, sizeof(m_trakName), | |
2085 "moov.trak[%u].%s", trakIndex, name); | |
2086 } | |
2087 return m_trakName; | |
2088 } | |
2089 | |
2090 u_int64_t MP4File::GetTrackIntegerProperty(MP4TrackId trackId, const char* name) | |
2091 { | |
2092 return GetIntegerProperty(MakeTrackName(trackId, name)); | |
2093 } | |
2094 | |
2095 void MP4File::SetTrackIntegerProperty(MP4TrackId trackId, const char* name, | |
2096 int64_t value) | |
2097 { | |
2098 SetIntegerProperty(MakeTrackName(trackId, name), value); | |
2099 } | |
2100 | |
2101 float MP4File::GetTrackFloatProperty(MP4TrackId trackId, const char* name) | |
2102 { | |
2103 return GetFloatProperty(MakeTrackName(trackId, name)); | |
2104 } | |
2105 | |
2106 void MP4File::SetTrackFloatProperty(MP4TrackId trackId, const char* name, | |
2107 float value) | |
2108 { | |
2109 SetFloatProperty(MakeTrackName(trackId, name), value); | |
2110 } | |
2111 | |
2112 const char* MP4File::GetTrackStringProperty(MP4TrackId trackId, const char* name) | |
2113 { | |
2114 return GetStringProperty(MakeTrackName(trackId, name)); | |
2115 } | |
2116 | |
2117 void MP4File::SetTrackStringProperty(MP4TrackId trackId, const char* name, | |
2118 const char* value) | |
2119 { | |
2120 SetStringProperty(MakeTrackName(trackId, name), value); | |
2121 } | |
2122 | |
2123 void MP4File::GetTrackBytesProperty(MP4TrackId trackId, const char* name, | |
2124 u_int8_t** ppValue, u_int32_t* pValueSize) | |
2125 { | |
2126 GetBytesProperty(MakeTrackName(trackId, name), ppValue, pValueSize); | |
2127 } | |
2128 | |
2129 void MP4File::SetTrackBytesProperty(MP4TrackId trackId, const char* name, | |
2130 const u_int8_t* pValue, u_int32_t valueSize) | |
2131 { | |
2132 SetBytesProperty(MakeTrackName(trackId, name), pValue, valueSize); | |
2133 } | |
2134 | |
2135 | |
2136 // file level convenience functions | |
2137 | |
2138 MP4Duration MP4File::GetDuration() | |
2139 { | |
2140 return m_pDurationProperty->GetValue(); | |
2141 } | |
2142 | |
2143 void MP4File::SetDuration(MP4Duration value) | |
2144 { | |
2145 m_pDurationProperty->SetValue(value); | |
2146 } | |
2147 | |
2148 u_int32_t MP4File::GetTimeScale() | |
2149 { | |
2150 return m_pTimeScaleProperty->GetValue(); | |
2151 } | |
2152 | |
2153 void MP4File::SetTimeScale(u_int32_t value) | |
2154 { | |
2155 if (value == 0) { | |
2156 throw new MP4Error("invalid value", "SetTimeScale"); | |
2157 } | |
2158 m_pTimeScaleProperty->SetValue(value); | |
2159 } | |
2160 | |
2161 u_int8_t MP4File::GetODProfileLevel() | |
2162 { | |
2163 return GetIntegerProperty("moov.iods.ODProfileLevelId"); | |
2164 } | |
2165 | |
2166 void MP4File::SetODProfileLevel(u_int8_t value) | |
2167 { | |
2168 SetIntegerProperty("moov.iods.ODProfileLevelId", value); | |
2169 } | |
2170 | |
2171 u_int8_t MP4File::GetSceneProfileLevel() | |
2172 { | |
2173 return GetIntegerProperty("moov.iods.sceneProfileLevelId"); | |
2174 } | |
2175 | |
2176 void MP4File::SetSceneProfileLevel(u_int8_t value) | |
2177 { | |
2178 SetIntegerProperty("moov.iods.sceneProfileLevelId", value); | |
2179 } | |
2180 | |
2181 u_int8_t MP4File::GetVideoProfileLevel() | |
2182 { | |
2183 return GetIntegerProperty("moov.iods.visualProfileLevelId"); | |
2184 } | |
2185 | |
2186 void MP4File::SetVideoProfileLevel(u_int8_t value) | |
2187 { | |
2188 SetIntegerProperty("moov.iods.visualProfileLevelId", value); | |
2189 } | |
2190 | |
2191 u_int8_t MP4File::GetAudioProfileLevel() | |
2192 { | |
2193 return GetIntegerProperty("moov.iods.audioProfileLevelId"); | |
2194 } | |
2195 | |
2196 void MP4File::SetAudioProfileLevel(u_int8_t value) | |
2197 { | |
2198 SetIntegerProperty("moov.iods.audioProfileLevelId", value); | |
2199 } | |
2200 | |
2201 u_int8_t MP4File::GetGraphicsProfileLevel() | |
2202 { | |
2203 return GetIntegerProperty("moov.iods.graphicsProfileLevelId"); | |
2204 } | |
2205 | |
2206 void MP4File::SetGraphicsProfileLevel(u_int8_t value) | |
2207 { | |
2208 SetIntegerProperty("moov.iods.graphicsProfileLevelId", value); | |
2209 } | |
2210 | |
2211 const char* MP4File::GetSessionSdp() | |
2212 { | |
2213 return GetStringProperty("moov.udta.hnti.rtp .sdpText"); | |
2214 } | |
2215 | |
2216 void MP4File::SetSessionSdp(const char* sdpString) | |
2217 { | |
2218 AddDescendantAtoms("moov", "udta.hnti.rtp "); | |
2219 | |
2220 SetStringProperty("moov.udta.hnti.rtp .sdpText", sdpString); | |
2221 } | |
2222 | |
2223 void MP4File::AppendSessionSdp(const char* sdpFragment) | |
2224 { | |
2225 const char* oldSdpString = NULL; | |
2226 try { | |
2227 oldSdpString = GetSessionSdp(); | |
2228 } | |
2229 catch (MP4Error* e) { | |
2230 delete e; | |
2231 SetSessionSdp(sdpFragment); | |
2232 return; | |
2233 } | |
2234 | |
2235 char* newSdpString = | |
2236 (char*)MP4Malloc(strlen(oldSdpString) + strlen(sdpFragment) + 1); | |
2237 strcpy(newSdpString, oldSdpString); | |
2238 strcat(newSdpString, sdpFragment); | |
2239 SetSessionSdp(newSdpString); | |
2240 MP4Free(newSdpString); | |
2241 } | |
2242 | |
2243 | |
2244 // track level convenience functions | |
2245 | |
2246 MP4SampleId MP4File::GetTrackNumberOfSamples(MP4TrackId trackId) | |
2247 { | |
2248 return m_pTracks[FindTrackIndex(trackId)]->GetNumberOfSamples(); | |
2249 } | |
2250 | |
2251 const char* MP4File::GetTrackType(MP4TrackId trackId) | |
2252 { | |
2253 return m_pTracks[FindTrackIndex(trackId)]->GetType(); | |
2254 } | |
2255 | |
2256 const char *MP4File::GetTrackMediaDataName (MP4TrackId trackId) | |
2257 { | |
2258 MP4Atom *pChild; | |
2259 MP4Atom *pAtom = | |
2260 FindAtom(MakeTrackName(trackId, | |
2261 "mdia.minf.stbl.stsd")); | |
2262 if (pAtom->GetNumberOfChildAtoms() != 1) { | |
2263 VERBOSE_ERROR(m_verbosity, | |
2264 fprintf(stderr, "track %d has more than 1 child atoms in stsd\n", trackId)); | |
2265 return NULL; | |
2266 } | |
2267 pChild = pAtom->GetChildAtom(0); | |
2268 return pChild->GetType(); | |
2269 } | |
2270 | |
2271 | |
2272 u_int32_t MP4File::GetTrackTimeScale(MP4TrackId trackId) | |
2273 { | |
2274 return m_pTracks[FindTrackIndex(trackId)]->GetTimeScale(); | |
2275 } | |
2276 | |
2277 void MP4File::SetTrackTimeScale(MP4TrackId trackId, u_int32_t value) | |
2278 { | |
2279 if (value == 0) { | |
2280 throw new MP4Error("invalid value", "SetTrackTimeScale"); | |
2281 } | |
2282 SetTrackIntegerProperty(trackId, "mdia.mdhd.timeScale", value); | |
2283 } | |
2284 | |
2285 MP4Duration MP4File::GetTrackDuration(MP4TrackId trackId) | |
2286 { | |
2287 return GetTrackIntegerProperty(trackId, "mdia.mdhd.duration"); | |
2288 } | |
2289 | |
2290 u_int8_t MP4File::GetTrackEsdsObjectTypeId(MP4TrackId trackId) | |
2291 { | |
2292 // changed mp4a to * to handle enca case | |
2293 return GetTrackIntegerProperty(trackId, | |
2294 "mdia.minf.stbl.stsd.*.esds.decConfigDescr.objectTypeId"); | |
2295 } | |
2296 | |
2297 u_int8_t MP4File::GetTrackAudioMpeg4Type(MP4TrackId trackId) | |
2298 { | |
2299 // verify that track is an MPEG-4 audio track | |
2300 if (GetTrackEsdsObjectTypeId(trackId) != MP4_MPEG4_AUDIO_TYPE) { | |
2301 return MP4_MPEG4_INVALID_AUDIO_TYPE; | |
2302 } | |
2303 | |
2304 u_int8_t* pEsConfig = NULL; | |
2305 u_int32_t esConfigSize; | |
2306 | |
2307 // The Mpeg4 audio type (AAC, CELP, HXVC, ...) | |
2308 // is the first 5 bits of the ES configuration | |
2309 | |
2310 GetTrackESConfiguration(trackId, &pEsConfig, &esConfigSize); | |
2311 | |
2312 if (esConfigSize < 1) { | |
2313 return MP4_MPEG4_INVALID_AUDIO_TYPE; | |
2314 } | |
2315 | |
2316 u_int8_t mpeg4Type = (pEsConfig[0] >> 3); | |
2317 | |
2318 free(pEsConfig); | |
2319 | |
2320 return mpeg4Type; | |
2321 } | |
2322 | |
2323 | |
2324 MP4Duration MP4File::GetTrackFixedSampleDuration(MP4TrackId trackId) | |
2325 { | |
2326 return m_pTracks[FindTrackIndex(trackId)]->GetFixedSampleDuration(); | |
2327 } | |
2328 | |
199
0a2ad94e8607
[svn] Synced with bmp-mp4. Build system is fragile, but should work now.
chainsaw
parents:
61
diff
changeset
|
2329 float MP4File::GetTrackVideoFrameRate(MP4TrackId trackId) |
61 | 2330 { |
2331 MP4SampleId numSamples = | |
2332 GetTrackNumberOfSamples(trackId); | |
2333 u_int64_t | |
2334 msDuration = | |
2335 ConvertFromTrackDuration(trackId, | |
2336 GetTrackDuration(trackId), MP4_MSECS_TIME_SCALE); | |
2337 | |
2338 if (msDuration == 0) { | |
2339 return 0.0; | |
2340 } | |
2341 | |
199
0a2ad94e8607
[svn] Synced with bmp-mp4. Build system is fragile, but should work now.
chainsaw
parents:
61
diff
changeset
|
2342 return ((double)numSamples / UINT64_TO_DOUBLE(msDuration)) * MP4_MSECS_TIME_SCALE; |
61 | 2343 } |
2344 | |
2345 int MP4File::GetTrackAudioChannels (MP4TrackId trackId) | |
2346 { | |
2347 return GetTrackIntegerProperty(trackId, | |
2348 "mdia.minf.stbl.stsd.*[0].channels"); | |
2349 } | |
2350 | |
2351 // true if media track encrypted according to ismacryp | |
2352 bool MP4File::IsIsmaCrypMediaTrack(MP4TrackId trackId) | |
2353 { | |
2354 if (GetTrackIntegerProperty(trackId, | |
2355 "mdia.minf.stbl.stsd.*.sinf.frma.data-format") | |
2356 != (u_int64_t)-1) { | |
2357 return true; | |
2358 } | |
2359 return false; | |
2360 } | |
2361 | |
2362 | |
2363 void MP4File::GetTrackESConfiguration(MP4TrackId trackId, | |
2364 u_int8_t** ppConfig, u_int32_t* pConfigSize) | |
2365 { | |
2366 GetTrackBytesProperty(trackId, | |
2367 "mdia.minf.stbl.stsd.*[0].esds.decConfigDescr.decSpecificInfo[0].info", | |
2368 ppConfig, pConfigSize); | |
2369 } | |
2370 | |
2371 void MP4File::GetTrackVideoMetadata(MP4TrackId trackId, | |
2372 u_int8_t** ppConfig, u_int32_t* pConfigSize) | |
2373 { | |
2374 GetTrackBytesProperty(trackId, | |
2375 "mdia.minf.stbl.stsd.*[0].*.metadata", | |
2376 ppConfig, pConfigSize); | |
2377 } | |
2378 | |
2379 void MP4File::SetTrackESConfiguration(MP4TrackId trackId, | |
2380 const u_int8_t* pConfig, u_int32_t configSize) | |
2381 { | |
2382 // get a handle on the track decoder config descriptor | |
2383 MP4DescriptorProperty* pConfigDescrProperty = NULL; | |
2384 FindProperty(MakeTrackName(trackId, | |
2385 "mdia.minf.stbl.stsd.*[0].esds.decConfigDescr.decSpecificInfo"), | |
2386 (MP4Property**)&pConfigDescrProperty); | |
2387 | |
2388 if (pConfigDescrProperty == NULL) { | |
2389 // probably trackId refers to a hint track | |
2390 throw new MP4Error("no such property", "MP4SetTrackESConfiguration"); | |
2391 } | |
2392 | |
2393 // lookup the property to store the configuration | |
2394 MP4BytesProperty* pInfoProperty = NULL; | |
2395 pConfigDescrProperty->FindProperty("decSpecificInfo[0].info", | |
2396 (MP4Property**)&pInfoProperty); | |
2397 | |
2398 // configuration being set for the first time | |
2399 if (pInfoProperty == NULL) { | |
2400 // need to create a new descriptor to hold it | |
2401 MP4Descriptor* pConfigDescr = | |
2402 pConfigDescrProperty->AddDescriptor(MP4DecSpecificDescrTag); | |
2403 pConfigDescr->Generate(); | |
2404 | |
2405 pConfigDescrProperty->FindProperty( | |
2406 "decSpecificInfo[0].info", | |
2407 (MP4Property**)&pInfoProperty); | |
2408 ASSERT(pInfoProperty); | |
2409 } | |
2410 | |
2411 // set the value | |
2412 pInfoProperty->SetValue(pConfig, configSize); | |
2413 } | |
2414 | |
2415 void MP4File::GetTrackH264ProfileLevel (MP4TrackId trackId, | |
2416 uint8_t *pProfile, | |
2417 uint8_t *pLevel) | |
2418 { | |
2419 *pProfile = | |
2420 GetTrackIntegerProperty(trackId, | |
2421 "mdia.minf.stbl.stsd.*[0].avcC.AVCProfileIndication"); | |
2422 *pLevel = | |
2423 GetTrackIntegerProperty(trackId, | |
2424 "mdia.minf.stbl.stsd.*[0].avcC.AVCLevelIndication"); | |
2425 } | |
2426 void MP4File::GetTrackH264LengthSize (MP4TrackId trackId, | |
2427 uint32_t *pLength) | |
2428 { | |
2429 *pLength = 1 + | |
2430 GetTrackIntegerProperty(trackId, | |
2431 "mdia.minf.stbl.stsd.*[0].avcC.lengthSizeMinusOne"); | |
2432 } | |
2433 | |
2434 bool MP4File::GetTrackH264SeqPictHeaders (MP4TrackId trackId, | |
2435 uint8_t ***pppSeqHeader, | |
2436 uint32_t **ppSeqHeaderSize, | |
2437 uint8_t ***pppPictHeader, | |
2438 uint32_t **ppPictHeaderSize) | |
2439 { | |
2440 uint32_t count; | |
2441 | |
2442 MP4Atom *avcCAtom = | |
2443 FindAtom(MakeTrackName(trackId, | |
2444 "mdia.minf.stbl.stsd.avc1.avcC")); | |
2445 MP4BitfieldProperty *pSeqCount; | |
2446 MP4IntegerProperty *pSeqLen, *pPictCount, *pPictLen; | |
2447 MP4BytesProperty *pSeqVal, *pPictVal; | |
2448 if ((avcCAtom->FindProperty("avcC.numOfSequenceParameterSets", | |
2449 (MP4Property **)&pSeqCount) == false) || | |
2450 (avcCAtom->FindProperty("avcC.sequenceEntries.sequenceParameterSetLength", | |
2451 (MP4Property **)&pSeqLen) == false) || | |
2452 (avcCAtom->FindProperty("avcC.sequenceEntries.sequenceParameterSetNALUnit", | |
2453 (MP4Property **)&pSeqVal) == false)) { | |
2454 VERBOSE_ERROR(m_verbosity, WARNING("Could not find avcC properties")); | |
2455 return false; | |
2456 } | |
2457 uint8_t **ppSeqHeader = | |
2458 (uint8_t **)malloc((pSeqCount->GetValue() + 1) * sizeof(uint8_t *)); | |
2459 *pppSeqHeader = ppSeqHeader; | |
2460 | |
2461 uint32_t *pSeqHeaderSize = | |
2462 (uint32_t *)malloc((pSeqCount->GetValue() + 1) * sizeof(uint32_t *)); | |
2463 | |
2464 *ppSeqHeaderSize = pSeqHeaderSize; | |
2465 for (count = 0; count < pSeqCount->GetValue(); count++) { | |
2466 pSeqVal->GetValue(&(ppSeqHeader[count]), &(pSeqHeaderSize[count])); | |
2467 } | |
2468 ppSeqHeader[count] = NULL; | |
2469 pSeqHeaderSize[count] = 0; | |
2470 | |
2471 if ((avcCAtom->FindProperty("avcC.numOfPictureParameterSets", | |
2472 (MP4Property **)&pPictCount) == false) || | |
2473 (avcCAtom->FindProperty("avcC.pictureEntries.pictureParameterSetLength", | |
2474 (MP4Property **)&pPictLen) == false) || | |
2475 (avcCAtom->FindProperty("avcC.pictureEntries.pictureParameterSetNALUnit", | |
2476 (MP4Property **)&pPictVal) == false)) { | |
2477 VERBOSE_ERROR(m_verbosity, | |
2478 WARNING("Could not find avcC picture table properties")); | |
2479 return false; | |
2480 } | |
2481 uint8_t | |
2482 **ppPictHeader = | |
2483 (uint8_t **)malloc((pPictCount->GetValue() + 1) * sizeof(uint8_t *)); | |
2484 uint32_t *pPictHeaderSize = | |
2485 (uint32_t *)malloc((pPictCount->GetValue() + 1)* sizeof(uint32_t *)); | |
2486 *pppPictHeader = ppPictHeader; | |
2487 *ppPictHeaderSize = pPictHeaderSize; | |
2488 | |
2489 for (count = 0; count < pPictCount->GetValue(); count++) { | |
2490 pPictVal->GetValue(&(ppPictHeader[count]), &(pPictHeaderSize[count])); | |
2491 } | |
2492 ppPictHeader[count] = NULL; | |
2493 pPictHeaderSize[count] = 0; | |
2494 return true; | |
2495 } | |
2496 | |
2497 | |
2498 | |
2499 const char* MP4File::GetHintTrackSdp(MP4TrackId hintTrackId) | |
2500 { | |
2501 return GetTrackStringProperty(hintTrackId, "udta.hnti.sdp .sdpText"); | |
2502 } | |
2503 | |
2504 void MP4File::SetHintTrackSdp(MP4TrackId hintTrackId, const char* sdpString) | |
2505 { | |
2506 MP4Track* pTrack = m_pTracks[FindTrackIndex(hintTrackId)]; | |
2507 | |
2508 if (strcmp(pTrack->GetType(), MP4_HINT_TRACK_TYPE)) { | |
2509 throw new MP4Error("track is not a hint track", | |
2510 "MP4SetHintTrackSdp"); | |
2511 } | |
2512 | |
2513 AddDescendantAtoms( | |
2514 MakeTrackName(hintTrackId, NULL), "udta.hnti.sdp "); | |
2515 | |
2516 SetTrackStringProperty(hintTrackId, "udta.hnti.sdp .sdpText", sdpString); | |
2517 } | |
2518 | |
2519 void MP4File::AppendHintTrackSdp(MP4TrackId hintTrackId, | |
2520 const char* sdpFragment) | |
2521 { | |
2522 const char* oldSdpString = NULL; | |
2523 try { | |
2524 oldSdpString = GetHintTrackSdp(hintTrackId); | |
2525 } | |
2526 catch (MP4Error* e) { | |
2527 delete e; | |
2528 SetHintTrackSdp(hintTrackId, sdpFragment); | |
2529 return; | |
2530 } | |
2531 | |
2532 char* newSdpString = | |
2533 (char*)MP4Malloc(strlen(oldSdpString) + strlen(sdpFragment) + 1); | |
2534 strcpy(newSdpString, oldSdpString); | |
2535 strcat(newSdpString, sdpFragment); | |
2536 SetHintTrackSdp(hintTrackId, newSdpString); | |
2537 MP4Free(newSdpString); | |
2538 } | |
2539 | |
2540 void MP4File::GetHintTrackRtpPayload( | |
2541 MP4TrackId hintTrackId, | |
2542 char** ppPayloadName, | |
2543 u_int8_t* pPayloadNumber, | |
2544 u_int16_t* pMaxPayloadSize, | |
2545 char **ppEncodingParams) | |
2546 { | |
2547 MP4Track* pTrack = m_pTracks[FindTrackIndex(hintTrackId)]; | |
2548 | |
2549 if (strcmp(pTrack->GetType(), MP4_HINT_TRACK_TYPE)) { | |
2550 throw new MP4Error("track is not a hint track", | |
2551 "MP4GetHintTrackRtpPayload"); | |
2552 } | |
2553 | |
2554 ((MP4RtpHintTrack*)pTrack)->GetPayload( | |
2555 ppPayloadName, pPayloadNumber, pMaxPayloadSize, ppEncodingParams); | |
2556 } | |
2557 | |
2558 void MP4File::SetHintTrackRtpPayload(MP4TrackId hintTrackId, | |
2559 const char* payloadName, u_int8_t* pPayloadNumber, u_int16_t maxPayloadSize, | |
2560 const char *encoding_params, | |
2561 bool include_rtp_map, | |
2562 bool include_mpeg4_esid) | |
2563 { | |
2564 MP4Track* pTrack = m_pTracks[FindTrackIndex(hintTrackId)]; | |
2565 | |
2566 if (strcmp(pTrack->GetType(), MP4_HINT_TRACK_TYPE)) { | |
2567 throw new MP4Error("track is not a hint track", | |
2568 "MP4SetHintTrackRtpPayload"); | |
2569 } | |
2570 | |
2571 u_int8_t payloadNumber; | |
2572 if (pPayloadNumber && *pPayloadNumber != MP4_SET_DYNAMIC_PAYLOAD) { | |
2573 payloadNumber = *pPayloadNumber; | |
2574 } else { | |
2575 payloadNumber = AllocRtpPayloadNumber(); | |
2576 if (pPayloadNumber) { | |
2577 *pPayloadNumber = payloadNumber; | |
2578 } | |
2579 } | |
2580 | |
2581 ((MP4RtpHintTrack*)pTrack)->SetPayload( | |
2582 payloadName, payloadNumber, maxPayloadSize, encoding_params, | |
2583 include_rtp_map, include_mpeg4_esid); | |
2584 } | |
2585 | |
2586 u_int8_t MP4File::AllocRtpPayloadNumber() | |
2587 { | |
2588 MP4Integer32Array usedPayloads; | |
2589 u_int32_t i; | |
2590 | |
2591 // collect rtp payload numbers in use by existing tracks | |
2592 for (i = 0; i < m_pTracks.Size(); i++) { | |
2593 MP4Atom* pTrakAtom = m_pTracks[i]->GetTrakAtom(); | |
2594 | |
2595 MP4Integer32Property* pPayloadProperty = NULL; | |
2596 pTrakAtom->FindProperty("trak.udta.hinf.payt.payloadNumber", | |
2597 (MP4Property**)&pPayloadProperty); | |
2598 | |
2599 if (pPayloadProperty) { | |
2600 usedPayloads.Add(pPayloadProperty->GetValue()); | |
2601 } | |
2602 } | |
2603 | |
2604 // search dynamic payload range for an available slot | |
2605 u_int8_t payload; | |
2606 for (payload = 96; payload < 128; payload++) { | |
2607 for (i = 0; i < usedPayloads.Size(); i++) { | |
2608 if (payload == usedPayloads[i]) { | |
2609 break; | |
2610 } | |
2611 } | |
2612 if (i == usedPayloads.Size()) { | |
2613 break; | |
2614 } | |
2615 } | |
2616 | |
2617 if (payload >= 128) { | |
2618 throw new MP4Error("no more available rtp payload numbers", | |
2619 "AllocRtpPayloadNumber"); | |
2620 } | |
2621 | |
2622 return payload; | |
2623 } | |
2624 | |
2625 MP4TrackId MP4File::GetHintTrackReferenceTrackId( | |
2626 MP4TrackId hintTrackId) | |
2627 { | |
2628 MP4Track* pTrack = m_pTracks[FindTrackIndex(hintTrackId)]; | |
2629 | |
2630 if (strcmp(pTrack->GetType(), MP4_HINT_TRACK_TYPE)) { | |
2631 throw new MP4Error("track is not a hint track", | |
2632 "MP4GetHintTrackReferenceTrackId"); | |
2633 } | |
2634 | |
2635 MP4Track* pRefTrack = ((MP4RtpHintTrack*)pTrack)->GetRefTrack(); | |
2636 | |
2637 if (pRefTrack == NULL) { | |
2638 return MP4_INVALID_TRACK_ID; | |
2639 } | |
2640 return pRefTrack->GetId(); | |
2641 } | |
2642 | |
2643 void MP4File::ReadRtpHint( | |
2644 MP4TrackId hintTrackId, | |
2645 MP4SampleId hintSampleId, | |
2646 u_int16_t* pNumPackets) | |
2647 { | |
2648 MP4Track* pTrack = m_pTracks[FindTrackIndex(hintTrackId)]; | |
2649 | |
2650 if (strcmp(pTrack->GetType(), MP4_HINT_TRACK_TYPE)) { | |
2651 throw new MP4Error("track is not a hint track", "MP4ReadRtpHint"); | |
2652 } | |
2653 ((MP4RtpHintTrack*)pTrack)-> | |
2654 ReadHint(hintSampleId, pNumPackets); | |
2655 } | |
2656 | |
2657 u_int16_t MP4File::GetRtpHintNumberOfPackets( | |
2658 MP4TrackId hintTrackId) | |
2659 { | |
2660 MP4Track* pTrack = m_pTracks[FindTrackIndex(hintTrackId)]; | |
2661 | |
2662 if (strcmp(pTrack->GetType(), MP4_HINT_TRACK_TYPE)) { | |
2663 throw new MP4Error("track is not a hint track", | |
2664 "MP4GetRtpHintNumberOfPackets"); | |
2665 } | |
2666 return ((MP4RtpHintTrack*)pTrack)->GetHintNumberOfPackets(); | |
2667 } | |
2668 | |
2669 int8_t MP4File::GetRtpPacketBFrame( | |
2670 MP4TrackId hintTrackId, | |
2671 u_int16_t packetIndex) | |
2672 { | |
2673 MP4Track* pTrack = m_pTracks[FindTrackIndex(hintTrackId)]; | |
2674 | |
2675 if (strcmp(pTrack->GetType(), MP4_HINT_TRACK_TYPE)) { | |
2676 throw new MP4Error("track is not a hint track", | |
2677 "MP4GetRtpHintBFrame"); | |
2678 } | |
2679 return ((MP4RtpHintTrack*)pTrack)->GetPacketBFrame(packetIndex); | |
2680 } | |
2681 | |
2682 int32_t MP4File::GetRtpPacketTransmitOffset( | |
2683 MP4TrackId hintTrackId, | |
2684 u_int16_t packetIndex) | |
2685 { | |
2686 MP4Track* pTrack = m_pTracks[FindTrackIndex(hintTrackId)]; | |
2687 | |
2688 if (strcmp(pTrack->GetType(), MP4_HINT_TRACK_TYPE)) { | |
2689 throw new MP4Error("track is not a hint track", | |
2690 "MP4GetRtpPacketTransmitOffset"); | |
2691 } | |
2692 return ((MP4RtpHintTrack*)pTrack)->GetPacketTransmitOffset(packetIndex); | |
2693 } | |
2694 | |
2695 void MP4File::ReadRtpPacket( | |
2696 MP4TrackId hintTrackId, | |
2697 u_int16_t packetIndex, | |
2698 u_int8_t** ppBytes, | |
2699 u_int32_t* pNumBytes, | |
2700 u_int32_t ssrc, | |
2701 bool includeHeader, | |
2702 bool includePayload) | |
2703 { | |
2704 MP4Track* pTrack = m_pTracks[FindTrackIndex(hintTrackId)]; | |
2705 | |
2706 if (strcmp(pTrack->GetType(), MP4_HINT_TRACK_TYPE)) { | |
2707 throw new MP4Error("track is not a hint track", "MP4ReadPacket"); | |
2708 } | |
2709 ((MP4RtpHintTrack*)pTrack)->ReadPacket( | |
2710 packetIndex, ppBytes, pNumBytes, | |
2711 ssrc, includeHeader, includePayload); | |
2712 } | |
2713 | |
2714 MP4Timestamp MP4File::GetRtpTimestampStart( | |
2715 MP4TrackId hintTrackId) | |
2716 { | |
2717 MP4Track* pTrack = m_pTracks[FindTrackIndex(hintTrackId)]; | |
2718 | |
2719 if (strcmp(pTrack->GetType(), MP4_HINT_TRACK_TYPE)) { | |
2720 throw new MP4Error("track is not a hint track", | |
2721 "MP4GetRtpTimestampStart"); | |
2722 } | |
2723 return ((MP4RtpHintTrack*)pTrack)->GetRtpTimestampStart(); | |
2724 } | |
2725 | |
2726 void MP4File::SetRtpTimestampStart( | |
2727 MP4TrackId hintTrackId, | |
2728 MP4Timestamp rtpStart) | |
2729 { | |
2730 MP4Track* pTrack = m_pTracks[FindTrackIndex(hintTrackId)]; | |
2731 | |
2732 if (strcmp(pTrack->GetType(), MP4_HINT_TRACK_TYPE)) { | |
2733 throw new MP4Error("track is not a hint track", | |
2734 "MP4SetRtpTimestampStart"); | |
2735 } | |
2736 ((MP4RtpHintTrack*)pTrack)->SetRtpTimestampStart(rtpStart); | |
2737 } | |
2738 | |
2739 void MP4File::AddRtpHint(MP4TrackId hintTrackId, | |
2740 bool isBframe, u_int32_t timestampOffset) | |
2741 { | |
2742 ProtectWriteOperation("MP4AddRtpHint"); | |
2743 | |
2744 MP4Track* pTrack = m_pTracks[FindTrackIndex(hintTrackId)]; | |
2745 | |
2746 if (strcmp(pTrack->GetType(), MP4_HINT_TRACK_TYPE)) { | |
2747 throw new MP4Error("track is not a hint track", "MP4AddRtpHint"); | |
2748 } | |
2749 ((MP4RtpHintTrack*)pTrack)->AddHint(isBframe, timestampOffset); | |
2750 } | |
2751 | |
2752 void MP4File::AddRtpPacket( | |
2753 MP4TrackId hintTrackId, bool setMbit, int32_t transmitOffset) | |
2754 { | |
2755 ProtectWriteOperation("MP4AddRtpPacket"); | |
2756 | |
2757 MP4Track* pTrack = m_pTracks[FindTrackIndex(hintTrackId)]; | |
2758 | |
2759 if (strcmp(pTrack->GetType(), MP4_HINT_TRACK_TYPE)) { | |
2760 throw new MP4Error("track is not a hint track", "MP4AddRtpPacket"); | |
2761 } | |
2762 ((MP4RtpHintTrack*)pTrack)->AddPacket(setMbit, transmitOffset); | |
2763 } | |
2764 | |
2765 void MP4File::AddRtpImmediateData(MP4TrackId hintTrackId, | |
2766 const u_int8_t* pBytes, u_int32_t numBytes) | |
2767 { | |
2768 ProtectWriteOperation("MP4AddRtpImmediateData"); | |
2769 | |
2770 MP4Track* pTrack = m_pTracks[FindTrackIndex(hintTrackId)]; | |
2771 | |
2772 if (strcmp(pTrack->GetType(), MP4_HINT_TRACK_TYPE)) { | |
2773 throw new MP4Error("track is not a hint track", | |
2774 "MP4AddRtpImmediateData"); | |
2775 } | |
2776 ((MP4RtpHintTrack*)pTrack)->AddImmediateData(pBytes, numBytes); | |
2777 } | |
2778 | |
2779 void MP4File::AddRtpSampleData(MP4TrackId hintTrackId, | |
2780 MP4SampleId sampleId, u_int32_t dataOffset, u_int32_t dataLength) | |
2781 { | |
2782 ProtectWriteOperation("MP4AddRtpSampleData"); | |
2783 | |
2784 MP4Track* pTrack = m_pTracks[FindTrackIndex(hintTrackId)]; | |
2785 | |
2786 if (strcmp(pTrack->GetType(), MP4_HINT_TRACK_TYPE)) { | |
2787 throw new MP4Error("track is not a hint track", | |
2788 "MP4AddRtpSampleData"); | |
2789 } | |
2790 ((MP4RtpHintTrack*)pTrack)->AddSampleData( | |
2791 sampleId, dataOffset, dataLength); | |
2792 } | |
2793 | |
2794 void MP4File::AddRtpESConfigurationPacket(MP4TrackId hintTrackId) | |
2795 { | |
2796 ProtectWriteOperation("MP4AddRtpESConfigurationPacket"); | |
2797 | |
2798 MP4Track* pTrack = m_pTracks[FindTrackIndex(hintTrackId)]; | |
2799 | |
2800 if (strcmp(pTrack->GetType(), MP4_HINT_TRACK_TYPE)) { | |
2801 throw new MP4Error("track is not a hint track", | |
2802 "MP4AddRtpESConfigurationPacket"); | |
2803 } | |
2804 ((MP4RtpHintTrack*)pTrack)->AddESConfigurationPacket(); | |
2805 } | |
2806 | |
2807 void MP4File::WriteRtpHint(MP4TrackId hintTrackId, | |
2808 MP4Duration duration, bool isSyncSample) | |
2809 { | |
2810 ProtectWriteOperation("MP4WriteRtpHint"); | |
2811 | |
2812 MP4Track* pTrack = m_pTracks[FindTrackIndex(hintTrackId)]; | |
2813 | |
2814 if (strcmp(pTrack->GetType(), MP4_HINT_TRACK_TYPE)) { | |
2815 throw new MP4Error("track is not a hint track", | |
2816 "MP4WriteRtpHint"); | |
2817 } | |
2818 ((MP4RtpHintTrack*)pTrack)->WriteHint(duration, isSyncSample); | |
2819 } | |
2820 | |
2821 u_int64_t MP4File::ConvertFromMovieDuration( | |
2822 MP4Duration duration, | |
2823 u_int32_t timeScale) | |
2824 { | |
2825 return MP4ConvertTime((u_int64_t)duration, | |
2826 GetTimeScale(), timeScale); | |
2827 } | |
2828 | |
2829 u_int64_t MP4File::ConvertFromTrackTimestamp( | |
2830 MP4TrackId trackId, | |
2831 MP4Timestamp timeStamp, | |
2832 u_int32_t timeScale) | |
2833 { | |
2834 return MP4ConvertTime(timeStamp, | |
2835 GetTrackTimeScale(trackId), timeScale); | |
2836 } | |
2837 | |
2838 MP4Timestamp MP4File::ConvertToTrackTimestamp( | |
2839 MP4TrackId trackId, | |
2840 u_int64_t timeStamp, | |
2841 u_int32_t timeScale) | |
2842 { | |
2843 return (MP4Timestamp)MP4ConvertTime(timeStamp, | |
2844 timeScale, GetTrackTimeScale(trackId)); | |
2845 } | |
2846 | |
2847 u_int64_t MP4File::ConvertFromTrackDuration( | |
2848 MP4TrackId trackId, | |
2849 MP4Duration duration, | |
2850 u_int32_t timeScale) | |
2851 { | |
2852 return MP4ConvertTime((u_int64_t)duration, | |
2853 GetTrackTimeScale(trackId), timeScale); | |
2854 } | |
2855 | |
2856 MP4Duration MP4File::ConvertToTrackDuration( | |
2857 MP4TrackId trackId, | |
2858 u_int64_t duration, | |
2859 u_int32_t timeScale) | |
2860 { | |
2861 return (MP4Duration)MP4ConvertTime(duration, | |
2862 timeScale, GetTrackTimeScale(trackId)); | |
2863 } | |
2864 | |
2865 u_int8_t MP4File::ConvertTrackTypeToStreamType(const char* trackType) | |
2866 { | |
2867 u_int8_t streamType; | |
2868 | |
2869 if (!strcmp(trackType, MP4_OD_TRACK_TYPE)) { | |
2870 streamType = MP4ObjectDescriptionStreamType; | |
2871 } else if (!strcmp(trackType, MP4_SCENE_TRACK_TYPE)) { | |
2872 streamType = MP4SceneDescriptionStreamType; | |
2873 } else if (!strcmp(trackType, MP4_CLOCK_TRACK_TYPE)) { | |
2874 streamType = MP4ClockReferenceStreamType; | |
2875 } else if (!strcmp(trackType, MP4_MPEG7_TRACK_TYPE)) { | |
2876 streamType = MP4Mpeg7StreamType; | |
2877 } else if (!strcmp(trackType, MP4_OCI_TRACK_TYPE)) { | |
2878 streamType = MP4OCIStreamType; | |
2879 } else if (!strcmp(trackType, MP4_IPMP_TRACK_TYPE)) { | |
2880 streamType = MP4IPMPStreamType; | |
2881 } else if (!strcmp(trackType, MP4_MPEGJ_TRACK_TYPE)) { | |
2882 streamType = MP4MPEGJStreamType; | |
2883 } else { | |
2884 streamType = MP4UserPrivateStreamType; | |
2885 } | |
2886 | |
2887 return streamType; | |
2888 } | |
2889 | |
2890 // edit list | |
2891 | |
2892 char* MP4File::MakeTrackEditName( | |
2893 MP4TrackId trackId, | |
2894 MP4EditId editId, | |
2895 const char* name) | |
2896 { | |
2897 char* trakName = MakeTrackName(trackId, NULL); | |
2898 | |
2899 if (m_editName == NULL) { | |
2900 m_editName = (char *)malloc(1024); | |
2901 if (m_editName == NULL) return NULL; | |
2902 } | |
2903 snprintf(m_editName, 1024, | |
2904 "%s.edts.elst.entries[%u].%s", | |
2905 trakName, editId - 1, name); | |
2906 return m_editName; | |
2907 } | |
2908 | |
2909 MP4EditId MP4File::AddTrackEdit( | |
2910 MP4TrackId trackId, | |
2911 MP4EditId editId) | |
2912 { | |
2913 ProtectWriteOperation("AddTrackEdit"); | |
2914 return m_pTracks[FindTrackIndex(trackId)]->AddEdit(editId); | |
2915 } | |
2916 | |
2917 void MP4File::DeleteTrackEdit( | |
2918 MP4TrackId trackId, | |
2919 MP4EditId editId) | |
2920 { | |
2921 ProtectWriteOperation("DeleteTrackEdit"); | |
2922 m_pTracks[FindTrackIndex(trackId)]->DeleteEdit(editId); | |
2923 } | |
2924 | |
2925 u_int32_t MP4File::GetTrackNumberOfEdits( | |
2926 MP4TrackId trackId) | |
2927 { | |
2928 return GetTrackIntegerProperty(trackId, "edts.elst.entryCount"); | |
2929 } | |
2930 | |
2931 MP4Duration MP4File::GetTrackEditTotalDuration( | |
2932 MP4TrackId trackId, | |
2933 MP4EditId editId) | |
2934 { | |
2935 return m_pTracks[FindTrackIndex(trackId)]->GetEditTotalDuration(editId); | |
2936 } | |
2937 | |
2938 MP4Timestamp MP4File::GetTrackEditStart( | |
2939 MP4TrackId trackId, | |
2940 MP4EditId editId) | |
2941 { | |
2942 return m_pTracks[FindTrackIndex(trackId)]->GetEditStart(editId); | |
2943 } | |
2944 | |
2945 MP4Timestamp MP4File::GetTrackEditMediaStart( | |
2946 MP4TrackId trackId, | |
2947 MP4EditId editId) | |
2948 { | |
2949 return GetIntegerProperty( | |
2950 MakeTrackEditName(trackId, editId, "mediaTime")); | |
2951 } | |
2952 | |
2953 void MP4File::SetTrackEditMediaStart( | |
2954 MP4TrackId trackId, | |
2955 MP4EditId editId, | |
2956 MP4Timestamp startTime) | |
2957 { | |
2958 SetIntegerProperty( | |
2959 MakeTrackEditName(trackId, editId, "mediaTime"), | |
2960 startTime); | |
2961 } | |
2962 | |
2963 MP4Duration MP4File::GetTrackEditDuration( | |
2964 MP4TrackId trackId, | |
2965 MP4EditId editId) | |
2966 { | |
2967 return GetIntegerProperty( | |
2968 MakeTrackEditName(trackId, editId, "segmentDuration")); | |
2969 } | |
2970 | |
2971 void MP4File::SetTrackEditDuration( | |
2972 MP4TrackId trackId, | |
2973 MP4EditId editId, | |
2974 MP4Duration duration) | |
2975 { | |
2976 SetIntegerProperty( | |
2977 MakeTrackEditName(trackId, editId, "segmentDuration"), | |
2978 duration); | |
2979 } | |
2980 | |
2981 bool MP4File::GetTrackEditDwell( | |
2982 MP4TrackId trackId, | |
2983 MP4EditId editId) | |
2984 { | |
2985 return (GetIntegerProperty( | |
2986 MakeTrackEditName(trackId, editId, "mediaRate")) == 0); | |
2987 } | |
2988 | |
2989 void MP4File::SetTrackEditDwell( | |
2990 MP4TrackId trackId, | |
2991 MP4EditId editId, | |
2992 bool dwell) | |
2993 { | |
2994 SetIntegerProperty( | |
2995 MakeTrackEditName(trackId, editId, "mediaRate"), | |
2996 (dwell ? 0 : 1)); | |
2997 } | |
2998 | |
2999 MP4SampleId MP4File::GetSampleIdFromEditTime( | |
3000 MP4TrackId trackId, | |
3001 MP4Timestamp when, | |
3002 MP4Timestamp* pStartTime, | |
3003 MP4Duration* pDuration) | |
3004 { | |
3005 return m_pTracks[FindTrackIndex(trackId)]->GetSampleIdFromEditTime( | |
3006 when, pStartTime, pDuration); | |
3007 } | |
3008 |