mp4track.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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
  16. * Copyright (C) Cisco Systems Inc. 2001 - 2004. All Rights Reserved.
  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 [email protected]
  26. * Ximpo Group Ltd. [email protected]
  27. */
  28. #ifndef __MP4_TRACK_INCLUDED__
  29. #define __MP4_TRACK_INCLUDED__
  30. #include "mp4property.h"
  31. // forward declarations
  32. class MP4File;
  33. class MP4Atom;
  34. class MP4Property;
  35. class MP4StringProperty;
  36. class MP4Track {
  37. public:
  38. MP4Track(MP4File* pFile, MP4Atom* pTrakAtom);
  39. virtual ~MP4Track();
  40. MP4TrackId GetId() {
  41. return m_trackId;
  42. }
  43. const char* GetType();
  44. void SetType(const char* type);
  45. MP4File* GetFile() {
  46. return m_pFile;
  47. }
  48. MP4Atom* GetTrakAtom() {
  49. return m_pTrakAtom;
  50. }
  51. void ReadSample(
  52. // input parameters
  53. MP4SampleId sampleId,
  54. // output parameters
  55. u_int8_t** ppBytes,
  56. u_int32_t* pNumBytes,
  57. MP4Timestamp* pStartTime = NULL,
  58. MP4Duration* pDuration = NULL,
  59. MP4Duration* pRenderingOffset = NULL,
  60. bool* pIsSyncSample = NULL);
  61. void WriteSample(
  62. const u_int8_t* pBytes,
  63. u_int32_t numBytes,
  64. MP4Duration duration = 0,
  65. MP4Duration renderingOffset = 0,
  66. bool isSyncSample = true);
  67. virtual void FinishWrite();
  68. u_int64_t GetDuration(); // in track timeScale units
  69. u_int32_t GetTimeScale();
  70. u_int32_t GetNumberOfSamples();
  71. u_int32_t GetSampleSize(MP4SampleId sampleId);
  72. u_int32_t GetMaxSampleSize();
  73. u_int64_t GetTotalOfSampleSizes();
  74. u_int32_t GetAvgBitrate(); // in bps
  75. u_int32_t GetMaxBitrate(); // in bps
  76. MP4Duration GetFixedSampleDuration();
  77. void SetFixedSampleDuration(MP4Duration duration);
  78. void GetSampleTimes(MP4SampleId sampleId,
  79. MP4Timestamp* pStartTime, MP4Duration* pDuration);
  80. bool IsSyncSample(MP4SampleId sampleId);
  81. MP4SampleId GetSampleIdFromTime(
  82. MP4Timestamp when,
  83. bool wantSyncSample = false,
  84. bool rewind = false);
  85. MP4ChunkId GetChunkIdFromTime(MP4Timestamp when);
  86. MP4Duration GetSampleRenderingOffset(MP4SampleId sampleId);
  87. void SetSampleRenderingOffset(MP4SampleId sampleId,
  88. MP4Duration renderingOffset);
  89. MP4EditId AddEdit(
  90. MP4EditId editId = MP4_INVALID_EDIT_ID);
  91. void DeleteEdit(
  92. MP4EditId editId);
  93. MP4Timestamp GetEditStart(
  94. MP4EditId editId);
  95. MP4Timestamp GetEditTotalDuration(
  96. MP4EditId editId);
  97. MP4SampleId GetSampleIdFromEditTime(
  98. MP4Timestamp editWhen,
  99. MP4Timestamp* pStartTime = NULL,
  100. MP4Duration* pDuration = NULL);
  101. // special operation for use during hint track packet assembly
  102. void ReadSampleFragment(
  103. MP4SampleId sampleId,
  104. u_int32_t sampleOffset,
  105. u_int16_t sampleLength,
  106. u_int8_t* pDest);
  107. // special operations for use during optimization
  108. u_int32_t GetNumberOfChunks();
  109. MP4Timestamp GetChunkTime(MP4ChunkId chunkId);
  110. void ReadChunk(MP4ChunkId chunkId,
  111. u_int8_t** ppChunk, u_int32_t* pChunkSize,
  112. MP4Timestamp* pStartTime = NULL,
  113. MP4Duration* pDuration = NULL);
  114. void RewriteChunk(MP4ChunkId chunkId,
  115. u_int8_t* pChunk, u_int32_t chunkSize);
  116. protected:
  117. bool InitEditListProperties();
  118. FILE* GetSampleFile(MP4SampleId sampleId);
  119. u_int64_t GetSampleFileOffset(MP4SampleId sampleId);
  120. u_int32_t GetSampleStscIndex(MP4SampleId sampleId);
  121. u_int32_t GetChunkStscIndex(MP4ChunkId chunkId);
  122. u_int32_t GetChunkSize(MP4ChunkId chunkId);
  123. u_int32_t GetSampleCttsIndex(MP4SampleId sampleId,
  124. MP4SampleId* pFirstSampleId = NULL);
  125. MP4SampleId GetNextSyncSample(MP4SampleId sampleId);
  126. MP4SampleId GetSyncSample(MP4SampleId sampleId, bool rewind = false);
  127. void UpdateSampleSizes(MP4SampleId sampleId,
  128. u_int32_t numBytes);
  129. bool IsChunkFull(MP4SampleId sampleId);
  130. void UpdateSampleToChunk(MP4SampleId sampleId,
  131. MP4ChunkId chunkId, u_int32_t samplesPerChunk);
  132. void UpdateChunkOffsets(u_int64_t chunkOffset);
  133. void UpdateSampleTimes(MP4Duration duration);
  134. void UpdateRenderingOffsets(MP4SampleId sampleId,
  135. MP4Duration renderingOffset);
  136. void UpdateSyncSamples(MP4SampleId sampleId,
  137. bool isSyncSample);
  138. MP4Atom* AddAtom(char* parentName, char* childName);
  139. void UpdateDurations(MP4Duration duration);
  140. MP4Duration ToMovieDuration(MP4Duration trackDuration);
  141. void UpdateModificationTimes();
  142. void WriteChunkBuffer();
  143. void CalculateBytesPerSample();
  144. protected:
  145. MP4File* m_pFile;
  146. MP4Atom* m_pTrakAtom; // moov.trak[]
  147. MP4TrackId m_trackId; // moov.trak[].tkhd.trackId
  148. MP4StringProperty* m_pTypeProperty; // moov.trak[].mdia.hdlr.handlerType
  149. u_int32_t m_lastStsdIndex;
  150. FILE* m_lastSampleFile;
  151. // for efficient construction of hint track packets
  152. MP4SampleId m_cachedReadSampleId;
  153. u_int8_t* m_pCachedReadSample;
  154. u_int32_t m_cachedReadSampleSize;
  155. // for writing
  156. MP4SampleId m_writeSampleId;
  157. MP4Duration m_fixedSampleDuration;
  158. u_int8_t* m_pChunkBuffer;
  159. u_int32_t m_chunkBufferSize;
  160. u_int32_t m_chunkSamples;
  161. MP4Duration m_chunkDuration;
  162. // controls for chunking
  163. u_int32_t m_samplesPerChunk;
  164. MP4Duration m_durationPerChunk;
  165. u_int32_t m_bytesPerSample;
  166. // controls for AMR chunking
  167. int m_isAmr;
  168. u_int8_t m_curMode;
  169. MP4Integer32Property* m_pTimeScaleProperty;
  170. MP4IntegerProperty* m_pTrackDurationProperty; // 32 or 64 bits
  171. MP4IntegerProperty* m_pMediaDurationProperty; // 32 or 64 bits
  172. MP4IntegerProperty* m_pTrackModificationProperty; // 32 or 64 bits
  173. MP4IntegerProperty* m_pMediaModificationProperty; // 32 or 64 bits
  174. MP4Integer32Property* m_pStszFixedSampleSizeProperty;
  175. MP4Integer32Property* m_pStszSampleCountProperty;
  176. void SampleSizePropertyAddValue(uint32_t bytes);
  177. uint8_t m_stsz_sample_bits;
  178. bool m_have_stz2_4bit_sample;
  179. uint8_t m_stz2_4bit_sample_value;
  180. MP4IntegerProperty* m_pStszSampleSizeProperty;
  181. MP4Integer32Property* m_pStscCountProperty;
  182. MP4Integer32Property* m_pStscFirstChunkProperty;
  183. MP4Integer32Property* m_pStscSamplesPerChunkProperty;
  184. MP4Integer32Property* m_pStscSampleDescrIndexProperty;
  185. MP4Integer32Property* m_pStscFirstSampleProperty;
  186. MP4Integer32Property* m_pChunkCountProperty;
  187. MP4IntegerProperty* m_pChunkOffsetProperty; // 32 or 64 bits
  188. MP4Integer32Property* m_pSttsCountProperty;
  189. MP4Integer32Property* m_pSttsSampleCountProperty;
  190. MP4Integer32Property* m_pSttsSampleDeltaProperty;
  191. // for improve sequental timestamp index access
  192. u_int32_t m_cachedSttsIndex;
  193. MP4SampleId m_cachedSttsSid;
  194. MP4Timestamp m_cachedSttsElapsed;
  195. MP4Integer32Property* m_pCttsCountProperty;
  196. MP4Integer32Property* m_pCttsSampleCountProperty;
  197. MP4Integer32Property* m_pCttsSampleOffsetProperty;
  198. MP4Integer32Property* m_pStssCountProperty;
  199. MP4Integer32Property* m_pStssSampleProperty;
  200. MP4Integer32Property* m_pElstCountProperty;
  201. MP4IntegerProperty* m_pElstMediaTimeProperty; // 32 or 64 bits
  202. MP4IntegerProperty* m_pElstDurationProperty; // 32 or 64 bits
  203. MP4Integer16Property* m_pElstRateProperty;
  204. MP4Integer16Property* m_pElstReservedProperty;
  205. };
  206. MP4ARRAY_DECL(MP4Track, MP4Track*);
  207. #endif /* __MP4_TRACK_INCLUDED__ */