File: Ap4FragmentSampleTable.cpp

package info (click to toggle)
kodi-inputstream-adaptive 2.6.14%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 4,036 kB
  • sloc: cpp: 53,019; ansic: 492; makefile: 10
file content (335 lines) | stat: -rw-r--r-- 12,417 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*****************************************************************
|
|    AP4 - Fragment Based Sample Tables
|
|    Copyright 2002-2009 Axiomatic Systems, LLC
|
|
|    This atom is part of AP4 (MP4 Audio Processing Library).
|
|    Unless you have obtained Bento4 under a difference license,
|    this version of Bento4 is Bento4|GPL.
|    Bento4|GPL is free software; you can redistribute it and/or modify
|    it under the terms of the GNU General Public License as published by
|    the Free Software Foundation; either version 2, or (at your option)
|    any later version.
|
|    Bento4|GPL is distributed in the hope that it will be useful,
|    but WITHOUT ANY WARRANTY; without even the implied warranty of
|    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
|    GNU General Public License for more details.
|
|    You should have received a copy of the GNU General Public License
|    along with Bento4|GPL; see the atom COPYING.  If not, write to the
|    Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
|    02111-1307, USA.
|
 ****************************************************************/

/*----------------------------------------------------------------------
|   includes
+---------------------------------------------------------------------*/
#include "Ap4FragmentSampleTable.h"
#include "Ap4ByteStream.h"
#include "Ap4Sample.h"
#include "Ap4TrexAtom.h"
#include "Ap4TfhdAtom.h"
#include "Ap4ContainerAtom.h"
#include "Ap4TrunAtom.h"
#include "Ap4TfdtAtom.h"
#include "Ap4MovieFragment.h"

/*----------------------------------------------------------------------
|   AP4_FragmentSampleTable::AP4_FragmentSampleTable
+---------------------------------------------------------------------*/
AP4_FragmentSampleTable::AP4_FragmentSampleTable(AP4_ContainerAtom* traf, 
  AP4_TrexAtom*      trex,
  AP4_Cardinal       internal_track_id,
  AP4_ByteStream*    sample_stream,
  AP4_Position       moof_offset,
  AP4_Position       mdat_payload_offset,
  AP4_UI64           mdat_payload_size,
  AP4_UI64           dts_origin)
  : m_Duration(0)
  , m_InternalTrackId(internal_track_id)
{
    AP4_TfhdAtom* tfhd = AP4_DYNAMIC_CAST(AP4_TfhdAtom, traf->GetChild(AP4_ATOM_TYPE_TFHD));
    if (tfhd == NULL) return;
    
    // count all the samples and reserve space for them
    unsigned int sample_count = 0;
    for (AP4_List<AP4_Atom>::Item* item = traf->GetChildren().FirstItem();
                                   item;
                                   item = item->GetNext()) {
        AP4_Atom* atom = item->GetData();
        if (atom->GetType() == AP4_ATOM_TYPE_TRUN) {
            AP4_TrunAtom* trun = AP4_DYNAMIC_CAST(AP4_TrunAtom, atom);
            if (trun) sample_count += trun->GetEntries().ItemCount();
        }
    }    
    m_Samples.EnsureCapacity(sample_count);
    
    // check if we have a timecode base
    AP4_TfdtAtom* tfdt = AP4_DYNAMIC_CAST(AP4_TfdtAtom, traf->GetChild(AP4_ATOM_TYPE_TFDT));
    if (tfdt) {
        dts_origin = tfdt->GetBaseMediaDecodeTime();
    }
    
    // process all the trun atoms
    AP4_UI32 trun_flags(0);
    for (AP4_List<AP4_Atom>::Item* item = traf->GetChildren().FirstItem();
                                   item;
                                   item = item->GetNext()) {
        AP4_Atom* atom = item->GetData();
        if (atom->GetType() == AP4_ATOM_TYPE_TRUN) {
            AP4_TrunAtom* trun = AP4_DYNAMIC_CAST(AP4_TrunAtom, atom);
            if (trun) {
                AP4_Result result = AddTrun(trun, 
                                            tfhd, 
                                            trex, 
                                            sample_stream, 
                                            moof_offset,
                                            mdat_payload_offset,
                                            dts_origin);
                if (AP4_FAILED(result)) return;
                trun_flags |= trun->GetFlags();
            }
        }
    }
    // Hack if we have a single sample and default sample size is wrong (hbo ttml)
    if (m_Samples.ItemCount() == 1 && (trun_flags & AP4_TRUN_FLAG_SAMPLE_SIZE_PRESENT) == 0)
      m_Samples[0].SetSize(mdat_payload_size);
}

/*----------------------------------------------------------------------
|   AP4_FragmentSampleTable::~AP4_FragmentSampleTable
+---------------------------------------------------------------------*/
AP4_FragmentSampleTable::~AP4_FragmentSampleTable()
{
}

/*----------------------------------------------------------------------
|   AP4_FragmentSampleTable::AddTrun
+---------------------------------------------------------------------*/
AP4_Result
AP4_FragmentSampleTable::AddTrun(AP4_TrunAtom*   trun, 
                                 AP4_TfhdAtom*   tfhd, 
                                 AP4_TrexAtom*   trex,
                                 AP4_ByteStream* sample_stream,
                                 AP4_Position    moof_offset,
                                 AP4_Position&   payload_offset,
                                 AP4_UI64&       dts_origin)
{
    AP4_Flags tfhd_flags = tfhd->GetFlags();
    AP4_Flags trun_flags = trun->GetFlags();
    
    // update the number of samples
    unsigned int start = m_Samples.ItemCount();
    m_Samples.SetItemCount(start + trun->GetEntries().ItemCount());
        
    // base data offset
    AP4_Position data_offset = 0;
    if (tfhd_flags & AP4_TFHD_FLAG_BASE_DATA_OFFSET_PRESENT) {
        data_offset = tfhd->GetBaseDataOffset();
    } else {
        data_offset = moof_offset;
    }
    if (trun_flags & AP4_TRUN_FLAG_DATA_OFFSET_PRESENT) {
        data_offset += trun->GetDataOffset();
    }         
    // MS hack
    if (data_offset < payload_offset) {
        data_offset = payload_offset;
    } else {
        payload_offset = data_offset;
    }
        
    // sample description index
    AP4_UI32 sample_description_index = 0;
    if (tfhd_flags & AP4_TFHD_FLAG_SAMPLE_DESCRIPTION_INDEX_PRESENT) {
        sample_description_index = tfhd->GetSampleDescriptionIndex();
    } else if (trex) {
        sample_description_index = trex->GetDefaultSampleDescriptionIndex();
    }        
       
    // default sample size
    AP4_UI32 default_sample_size = 0;
    if (tfhd_flags & AP4_TFHD_FLAG_DEFAULT_SAMPLE_SIZE_PRESENT) {
        default_sample_size = tfhd->GetDefaultSampleSize();
    } else if (trex) {
        default_sample_size = trex->GetDefaultSampleSize();
    }
    
    // default sample duration
    AP4_UI32 default_sample_duration = 0;
    if (tfhd_flags & AP4_TFHD_FLAG_DEFAULT_SAMPLE_DURATION_PRESENT) {
        default_sample_duration = tfhd->GetDefaultSampleDuration();
    } else if (trex) {
        default_sample_duration = trex->GetDefaultSampleDuration();
    }
    
    // default sample flags
    AP4_UI32 default_sample_flags = 0;
    if (tfhd_flags & AP4_TFHD_FLAG_DEFAULT_SAMPLE_FLAGS_PRESENT) {
        default_sample_flags = tfhd->GetDefaultSampleFlags();
    } else if (trex) {
        default_sample_flags = trex->GetDefaultSampleFlags();
    }

    // parse all trun entries to setup the samples
    AP4_UI64 dts = dts_origin;
    m_Duration = 0;
    for (unsigned int i=0; i<trun->GetEntries().ItemCount(); i++) {
        const AP4_TrunAtom::Entry& entry  = trun->GetEntries()[i];
        AP4_Sample&                sample = m_Samples[start+i];
        
        // sample size
        if (trun_flags & AP4_TRUN_FLAG_SAMPLE_SIZE_PRESENT) {
            sample.SetSize(entry.sample_size);
        } else {
            sample.SetSize(default_sample_size);
        }
        payload_offset += sample.GetSize(); // update the payload offset
        
        // sample duration
        if (trun_flags & AP4_TRUN_FLAG_SAMPLE_DURATION_PRESENT) {
            sample.SetDuration(entry.sample_duration);
        } else {
            sample.SetDuration(default_sample_duration);
        }

        // sample flags
        AP4_UI32 sample_flags = default_sample_flags;
        if (i==0 && (trun_flags & AP4_TRUN_FLAG_FIRST_SAMPLE_FLAGS_PRESENT)) {
            sample_flags = trun->GetFirstSampleFlags();
        } else if (trun_flags & AP4_TRUN_FLAG_SAMPLE_FLAGS_PRESENT) {
            sample_flags = entry.sample_flags;
        }
        if ((sample_flags & AP4_FRAG_FLAG_SAMPLE_IS_DIFFERENCE) == 0) {
            sample.SetSync(true);
        } else {
            sample.SetSync(false);
        }
        
        // sample description index
        if (sample_description_index >= 1) {
            sample.SetDescriptionIndex(sample_description_index-1);
        }
        
        // data stream
        if (sample_stream) sample.SetDataStream(*sample_stream);
        
        // data offset
        sample.SetOffset(data_offset);
        data_offset += sample.GetSize();
        
        // dts and cts
        sample.SetDts(dts);
        if (trun_flags & AP4_TRUN_FLAG_SAMPLE_COMPOSITION_TIME_OFFSET_PRESENT) {
            sample.SetCtsDelta(entry.sample_composition_time_offset);
        }
        
        // update the counters
        dts        += sample.GetDuration();
        m_Duration += sample.GetDuration();
    }
    
    // update the dts
    dts_origin = dts;
    
    return AP4_SUCCESS;
}

/*----------------------------------------------------------------------
|   AP4_FragmentSampleTable::GetSample
+---------------------------------------------------------------------*/
AP4_Result
AP4_FragmentSampleTable::GetSample(AP4_Ordinal index, 
                                   AP4_Sample& sample)
{
    if (index >= m_Samples.ItemCount()) return AP4_ERROR_OUT_OF_RANGE;

    // copy the sample
    sample = m_Samples[index];

    return AP4_SUCCESS;
}

/*----------------------------------------------------------------------
|   AP4_FragmentSampleTable::GetSampleCount
+---------------------------------------------------------------------*/
AP4_Cardinal
AP4_FragmentSampleTable::GetSampleCount()
{
    return m_Samples.ItemCount();
}

/*----------------------------------------------------------------------
|   AP4_FragmentSampleTable::GetSampleDescription
+---------------------------------------------------------------------*/
AP4_SampleDescription*
AP4_FragmentSampleTable::GetSampleDescription(AP4_Ordinal /*index*/)
{
    return NULL; // FIXME
}

/*----------------------------------------------------------------------
|   AP4_FragmentSampleTable::GetSampleDescriptionCount
+---------------------------------------------------------------------*/
AP4_Cardinal
AP4_FragmentSampleTable::GetSampleDescriptionCount()
{
    return 1; // FIXME
}

/*----------------------------------------------------------------------
|   AP4_AtomSampleTable::GetSampleChunkPosition
+---------------------------------------------------------------------*/
AP4_Result   
AP4_FragmentSampleTable::GetSampleChunkPosition(AP4_Ordinal  sample_index, 
                                                AP4_Ordinal& chunk_index,
                                                AP4_Ordinal& position_in_chunk)
{
    chunk_index       = 0;
    position_in_chunk = sample_index;
    
    return AP4_SUCCESS;
}

/*----------------------------------------------------------------------
|   AP4_FragmentSampleTable::GetSampleIndexForTimeStamp
+---------------------------------------------------------------------*/
AP4_Result 
AP4_FragmentSampleTable::GetSampleIndexForTimeStamp(AP4_UI64     ts,
                                                    AP4_Ordinal& sample_index)
{
  if (!m_Samples.ItemCount())
    return AP4_ERROR_NOT_ENOUGH_DATA;

  sample_index = 0;
  while (sample_index < m_Samples.ItemCount() && m_Samples[sample_index].GetCts() + m_Samples[sample_index].GetDuration() < ts)
    ++sample_index;

  if (sample_index == m_Samples.ItemCount())
    return AP4_ERROR_NOT_ENOUGH_DATA;

  return AP4_SUCCESS;
}

/*----------------------------------------------------------------------
|   AP4_FragmentSampleTable::GetNearestSyncSampleIndex
+---------------------------------------------------------------------*/
AP4_Ordinal  
AP4_FragmentSampleTable::GetNearestSyncSampleIndex(AP4_Ordinal sample_index, bool before)
{
  if (sample_index >= m_Samples.ItemCount())
    return sample_index;

  AP4_Ordinal end(before ? 0 : m_Samples.ItemCount());

  while (sample_index != end && !m_Samples[sample_index].IsSync())
    sample_index = sample_index + (before ? -1 : 1);

  return sample_index;
}