• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* ------------------------------------------------------------------
2  * Copyright (C) 1998-2009 PacketVideo
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13  * express or implied.
14  * See the License for the specific language governing permissions
15  * and limitations under the License.
16  * -------------------------------------------------------------------
17  */
18 /*
19     This PVA_FF_ChunkOffsetAtom Class gives the index of each chunk into the
20     containing FILE.
21 */
22 
23 #define IMPLEMENT_ChunkOffsetAtom
24 
25 #include "chunkoffsetatom.h"
26 #include "atomutils.h"
27 #include "a_atomdefs.h"
28 
29 typedef Oscl_Vector<uint32, OsclMemAllocator> uint32VecType;
30 // Constructor
PVA_FF_ChunkOffsetAtom(uint32 mediaType,uint32 fileAuthoringFlags)31 PVA_FF_ChunkOffsetAtom::PVA_FF_ChunkOffsetAtom(uint32 mediaType,
32         uint32 fileAuthoringFlags)
33         : PVA_FF_FullAtom(FourCharConstToUint32('s', 't', 'c', 'o'), (uint8)0, (uint32)0),
34         _mediaType(mediaType)
35 {
36     _oInterLeaveMode = false;
37     _modified = true; // True for a new atom
38     _entryCount = 0;
39     _currentDataOffset = 0;
40 
41     PV_MP4_FF_NEW(fp->auditCB, uint32VecType, (), _pchunkOffsets);
42 
43     if (fileAuthoringFlags & PVMP4FF_SET_MEDIA_INTERLEAVE_MODE)
44     {
45         _oInterLeaveMode = true;
46     }
47 
48     recomputeSize();
49 }
50 
51 // Destructor
~PVA_FF_ChunkOffsetAtom()52 PVA_FF_ChunkOffsetAtom::~PVA_FF_ChunkOffsetAtom()
53 {
54     // Cleanup vector
55     PV_MP4_FF_TEMPLATED_DELETE(NULL, uint32VecType, Oscl_Vector, _pchunkOffsets);
56 }
57 
58 void
nextSample(uint32 size,bool isChunkStart,uint32 baseOffset)59 PVA_FF_ChunkOffsetAtom::nextSample(uint32 size,
60                                    bool isChunkStart, uint32 baseOffset)
61 {
62     // If sample is the start of a new chunk, add the current offset entry
63     if (isChunkStart)
64     {
65         if (_oInterLeaveMode)
66         {
67             addChunkOffset(baseOffset);
68             return;
69         }
70         else
71         {
72             addChunkOffset(_currentDataOffset);
73         }
74     }
75 
76     // Increment the size of the offset by the length of this sample
77     switch (_mediaType)
78     {
79         case MEDIA_TYPE_TEXT: //for timed text track
80         case MEDIA_TYPE_AUDIO:
81         case MEDIA_TYPE_VISUAL:
82         {
83             _currentDataOffset += size;
84         }
85         break;
86         case MEDIA_TYPE_UNKNOWN:
87         default:
88             break;
89     }
90 }
91 
92 // Adding to and getting first chunk offset values
93 void
addChunkOffset(uint32 offset)94 PVA_FF_ChunkOffsetAtom::addChunkOffset(uint32 offset)
95 {
96     _pchunkOffsets->push_back(offset);
97     _entryCount += 1;
98 
99     recomputeSize();
100 }
101 
102 
103 // Updating all the chunk entries based on the current file offset at which
104 // the corresponding mediaDataAtom MEDIA_DATA_ATOM gets rendered to disk
105 void
updateChunkEntries(uint32 fileOffset)106 PVA_FF_ChunkOffsetAtom::updateChunkEntries(uint32 fileOffset)
107 {
108     if (_modified)
109     {
110         for (uint32 i = 0; i < _pchunkOffsets->size(); i++)
111         {
112             (*_pchunkOffsets)[i] += fileOffset;
113         }
114         _modified = false;
115         return;
116     }
117 }
118 
119 
120 // Rendering the PVA_FF_Atom in proper format (bitlengths, etc.) to an ostream
121 bool
renderToFileStream(MP4_AUTHOR_FF_FILE_IO_WRAP * fp)122 PVA_FF_ChunkOffsetAtom::renderToFileStream(MP4_AUTHOR_FF_FILE_IO_WRAP *fp)
123 {
124     int32 rendered = 0;
125 
126     if (!renderAtomBaseMembers(fp))
127     {
128         return false;
129     }
130     rendered += getDefaultSize();
131 
132     if (!PVA_FF_AtomUtils::render32(fp, getEntryCount()))
133     {
134         return false;
135     }
136     rendered += 4;
137 
138     if (_pchunkOffsets->size() < getEntryCount())
139     {
140         return false;
141     }
142     for (uint32 i = 0; i < getEntryCount(); i++)
143     {
144         if (!PVA_FF_AtomUtils::render32(fp, (*_pchunkOffsets)[i]))
145         {
146             return false;
147         }
148     }
149     rendered += 4 * getEntryCount();
150 
151     return true;
152 }
153 
154 void
recomputeSize()155 PVA_FF_ChunkOffsetAtom::recomputeSize()
156 {
157     int size = getDefaultSize();
158     size += 4; // For entryCount
159     size += 4 * getEntryCount();
160 
161     _size = size;
162 
163     // Update the parent atom size
164     if (_pparent != NULL)
165     {
166         _pparent->recomputeSize();
167     }
168 }
169 
170 void
reAuthorFirstChunkOffset(uint32 offset)171 PVA_FF_ChunkOffsetAtom::reAuthorFirstChunkOffset(uint32 offset)
172 {
173     (*_pchunkOffsets)[0] = offset;
174 }
175