/***************************************************************** | | AP4 - stsc Atoms | | 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 "Ap4StscAtom.h" #include "Ap4AtomFactory.h" #include "Ap4Utils.h" /*---------------------------------------------------------------------- | dynamic cast support +---------------------------------------------------------------------*/ AP4_DEFINE_DYNAMIC_CAST_ANCHOR(AP4_StscAtom) /*---------------------------------------------------------------------- | AP4_StscAtom::Create +---------------------------------------------------------------------*/ AP4_StscAtom* AP4_StscAtom::Create(AP4_Size size, AP4_ByteStream& stream) { AP4_UI08 version; AP4_UI32 flags; if (size < AP4_FULL_ATOM_HEADER_SIZE) return NULL; if (AP4_FAILED(AP4_Atom::ReadFullHeader(stream, version, flags))) return NULL; if (version != 0) return NULL; return new AP4_StscAtom(size, version, flags, stream); } /*---------------------------------------------------------------------- | AP4_StscAtom::AP4_StscAtom +---------------------------------------------------------------------*/ AP4_StscAtom::AP4_StscAtom() : AP4_Atom(AP4_ATOM_TYPE_STSC, AP4_FULL_ATOM_HEADER_SIZE+4, 0, 0), m_CachedChunkGroup(0) { } /*---------------------------------------------------------------------- | AP4_StscAtom::AP4_StscAtom +---------------------------------------------------------------------*/ AP4_StscAtom::AP4_StscAtom(AP4_UI32 size, AP4_UI08 version, AP4_UI32 flags, AP4_ByteStream& stream) : AP4_Atom(AP4_ATOM_TYPE_STSC, size, version, flags), m_CachedChunkGroup(0) { AP4_UI32 first_sample = 1; AP4_UI32 entry_count; if (size - AP4_ATOM_HEADER_SIZE < 4) return; stream.ReadUI32(entry_count); if ((size - AP4_ATOM_HEADER_SIZE - 4)/12 < entry_count) { return; } m_Entries.SetItemCount(entry_count); unsigned char* buffer = new unsigned char[entry_count*12]; AP4_Result result = stream.Read(buffer, entry_count*12); if (AP4_FAILED(result)) { delete[] buffer; return; } for (unsigned int i=0; i 0); // decide whether to start the search from the cached index // or from the start AP4_Ordinal group; if (m_CachedChunkGroup < m_Entries.ItemCount() && m_Entries[m_CachedChunkGroup].m_FirstSample <= sample) { group = m_CachedChunkGroup; } else { group = 0; } // find which group of chunk contains this one while (group < m_Entries.ItemCount()) { AP4_Cardinal sample_count = m_Entries[group].m_ChunkCount*m_Entries[group].m_SamplesPerChunk; if (sample_count == 0) { // unlimited samples in this group (last group) if (m_Entries[group].m_FirstSample > sample) { // something is wrong return AP4_ERROR_INVALID_FORMAT; } } else { // normal group if (m_Entries[group].m_FirstSample + sample_count <= sample) { // the sample is not in this group group++; continue; } } // the sample is in this group if (m_Entries[group].m_SamplesPerChunk == 0) { // something is wrong return AP4_ERROR_INVALID_FORMAT; } unsigned int chunk_offset = ((sample-m_Entries[group].m_FirstSample) / m_Entries[group].m_SamplesPerChunk); chunk = m_Entries[group].m_FirstChunk + chunk_offset; skip = sample - (m_Entries[group].m_FirstSample + m_Entries[group].m_SamplesPerChunk*chunk_offset); sample_description_index = m_Entries[group].m_SampleDescriptionIndex; // cache the result (to accelerate finding the right group // next time around) m_CachedChunkGroup = group; return AP4_SUCCESS; } // chunk not found chunk = 0; skip = 0; sample_description_index = 0; return AP4_ERROR_OUT_OF_RANGE; } /*---------------------------------------------------------------------- | AP4_StscAtom::InspectFields +---------------------------------------------------------------------*/ AP4_Result AP4_StscAtom::InspectFields(AP4_AtomInspector& inspector) { inspector.AddField("entry_count", m_Entries.ItemCount()); // dump table entries if (inspector.GetVerbosity() >= 1) { inspector.StartArray("entries", m_Entries.ItemCount()); for (unsigned int i=0; i