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
|
/*****************************************************************
|
| AP4 - Synthetic Sample Table
|
| Copyright 2002-2008 Axiomatic Systems, LLC
|
|
| This file is part of Bento4/AP4 (MP4 Atom Processing Library).
|
| Unless you have obtained Bento4 under a difference license,
| this version of Bento4 is Bento4|GPL.
| Bento4|GPL is free software; you can redistribute it and/or modify
| it under the terms of the GNU General Public License as published by
| the Free Software Foundation; either version 2, or (at your option)
| any later version.
|
| Bento4|GPL is distributed in the hope that it will be useful,
| but WITHOUT ANY WARRANTY; without even the implied warranty of
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
| GNU General Public License for more details.
|
| You should have received a copy of the GNU General Public License
| along with Bento4|GPL; see the file COPYING. If not, write to the
| Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
| 02111-1307, USA.
|
****************************************************************/
/*----------------------------------------------------------------------
| includes
+---------------------------------------------------------------------*/
#include "Ap4Types.h"
#include "Ap4Atom.h"
#include "Ap4SyntheticSampleTable.h"
#include "Ap4Sample.h"
/*----------------------------------------------------------------------
| AP4_SyntheticSampleTable::AP4_SyntheticSampleTable()
+---------------------------------------------------------------------*/
AP4_SyntheticSampleTable::AP4_SyntheticSampleTable(AP4_Cardinal chunk_size) :
m_ChunkSize(chunk_size?chunk_size:AP4_SYNTHETIC_SAMPLE_TABLE_DEFAULT_CHUNK_SIZE)
{
m_LookupCache.m_Sample = 0;
m_LookupCache.m_Chunk = 0;
}
/*----------------------------------------------------------------------
| AP4_SyntheticSampleTable::~AP4_SyntheticSampleTable()
+---------------------------------------------------------------------*/
AP4_SyntheticSampleTable::~AP4_SyntheticSampleTable()
{
m_SampleDescriptions.DeleteReferences();
}
/*----------------------------------------------------------------------
| AP4_SyntheticSampleTable::GetSample
+---------------------------------------------------------------------*/
AP4_Result
AP4_SyntheticSampleTable::GetSample(AP4_Ordinal sample_index, AP4_Sample& sample)
{
if (sample_index >= m_Samples.ItemCount()) return AP4_ERROR_OUT_OF_RANGE;
sample = m_Samples[sample_index];
return AP4_SUCCESS;
}
/*----------------------------------------------------------------------
| AP4_SyntheticSampleTable::GetSampleCount
+---------------------------------------------------------------------*/
AP4_Cardinal
AP4_SyntheticSampleTable::GetSampleCount()
{
return m_Samples.ItemCount();
}
/*----------------------------------------------------------------------
| AP4_SyntheticSampleTable::GetSampleChunkPosition
+---------------------------------------------------------------------*/
AP4_Result
AP4_SyntheticSampleTable::GetSampleChunkPosition(
AP4_Ordinal sample_index,
AP4_Ordinal& chunk_index,
AP4_Ordinal& position_in_chunk)
{
// default values
chunk_index = 0;
position_in_chunk = 0;
// check parameters
if (sample_index >= m_Samples.ItemCount()) return AP4_ERROR_OUT_OF_RANGE;
// look for the chunk
AP4_Ordinal sample_cursor = 0;
AP4_Ordinal chunk_cursor = 0;
if (sample_index >= m_LookupCache.m_Sample) {
sample_cursor = m_LookupCache.m_Sample;
chunk_cursor = m_LookupCache.m_Chunk;
}
for (;
chunk_cursor < m_SamplesInChunk.ItemCount();
sample_cursor += m_SamplesInChunk[chunk_cursor++]) {
if (sample_cursor+m_SamplesInChunk[chunk_cursor] > sample_index) {
chunk_index = chunk_cursor;
position_in_chunk = sample_index-sample_cursor;
m_LookupCache.m_Sample = sample_cursor;
m_LookupCache.m_Chunk = chunk_cursor;
return AP4_SUCCESS;
}
}
return AP4_ERROR_OUT_OF_RANGE;
}
/*----------------------------------------------------------------------
| AP4_SyntheticSampleTable::GetSampleDescriptionCount
+---------------------------------------------------------------------*/
AP4_Cardinal
AP4_SyntheticSampleTable::GetSampleDescriptionCount()
{
return m_SampleDescriptions.ItemCount();
}
/*----------------------------------------------------------------------
| AP4_SyntheticSampleTable::GetSampleDescription
+---------------------------------------------------------------------*/
AP4_SampleDescription*
AP4_SyntheticSampleTable::GetSampleDescription(AP4_Ordinal index)
{
SampleDescriptionHolder* holder;
if (AP4_SUCCEEDED(m_SampleDescriptions.Get(index, holder))) {
return holder->m_SampleDescription;
} else {
return NULL;
}
}
/*----------------------------------------------------------------------
| AP4_SyntheticSampleTable::AddSampleDescription
+---------------------------------------------------------------------*/
AP4_Result
AP4_SyntheticSampleTable::AddSampleDescription(AP4_SampleDescription* description,
bool transfer_ownership)
{
return m_SampleDescriptions.Add(new SampleDescriptionHolder(description, transfer_ownership));
}
/*----------------------------------------------------------------------
| AP4_SyntheticSampleTable::AddSample
+---------------------------------------------------------------------*/
AP4_Result
AP4_SyntheticSampleTable::AddSample(AP4_ByteStream& data_stream,
AP4_Position offset,
AP4_Size size,
AP4_UI32 duration,
AP4_Ordinal description_index,
AP4_UI64 dts,
AP4_UI32 cts_delta,
bool sync)
{
// decide if we need to start a new chunk or increment the last one
if (m_SamplesInChunk.ItemCount() == 0 ||
m_SamplesInChunk[m_SamplesInChunk.ItemCount()-1] >= m_ChunkSize ||
m_Samples.ItemCount() == 0 ||
m_Samples[m_Samples.ItemCount()-1].GetDescriptionIndex() != description_index) {
m_SamplesInChunk.Append(1);
} else {
++m_SamplesInChunk[m_SamplesInChunk.ItemCount()-1];
}
// compute the timestamps
if (m_Samples.ItemCount() > 0) {
AP4_Sample* prev_sample = &m_Samples[m_Samples.ItemCount()-1];
if (dts == 0) {
if (prev_sample->GetDuration() == 0) {
// can't compute the DTS for this sample
return AP4_ERROR_INVALID_PARAMETERS;
}
dts = prev_sample->GetDts()+prev_sample->GetDuration();
} else {
if (prev_sample->GetDuration() == 0) {
// update the previous sample
if (dts <= prev_sample->GetDts()) {
return AP4_ERROR_INVALID_PARAMETERS;
}
prev_sample->SetDuration((AP4_UI32)(dts-prev_sample->GetDts()));
} else {
if (dts != prev_sample->GetDts()+prev_sample->GetDuration()) {
// mismatch
return AP4_ERROR_INVALID_PARAMETERS;
}
}
}
}
// add the sample to the table
AP4_Sample sample(data_stream, offset, size, duration, description_index, dts, cts_delta, sync);
return m_Samples.Append(sample);
}
/*----------------------------------------------------------------------
| AP4_SyntheticSampleTable::AddSample
+---------------------------------------------------------------------*/
AP4_Result
AP4_SyntheticSampleTable::AddSample(const AP4_Sample& sample)
{
return m_Samples.Append(sample);
}
/*----------------------------------------------------------------------
| AP4_SyntheticSampleTable::GetSampleIndexForTimeStamp
+---------------------------------------------------------------------*/
AP4_Result
AP4_SyntheticSampleTable::GetSampleIndexForTimeStamp(AP4_UI64 /* ts */,
AP4_Ordinal& /* index */)
{
return AP4_ERROR_NOT_SUPPORTED;
}
/*----------------------------------------------------------------------
| AP4_SyntheticSampleTable::GetNearestSyncSampleIndex
+---------------------------------------------------------------------*/
AP4_Ordinal
AP4_SyntheticSampleTable::GetNearestSyncSampleIndex(AP4_Ordinal sample_index, bool before)
{
if (before) {
for (int i=sample_index; i>=0; i--) {
if (m_Samples[i].IsSync()) return i;
}
// not found?
return 0;
} else {
AP4_Cardinal entry_count = m_Samples.ItemCount();
for (unsigned int i=sample_index; i<entry_count; i++) {
if (m_Samples[i].IsSync()) return i;
}
// not found?
return m_Samples.ItemCount();
}
}
|