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
|
/*****************************************************************
|
| AP4 - Fragment Parser Test
|
| Copyright 2002-2009 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 <stdio.h>
#include <stdlib.h>
#include "Ap4.h"
/*----------------------------------------------------------------------
| macros
+---------------------------------------------------------------------*/
#define CHECK(x) do { \
if (!(x)) { fprintf(stderr, "ERROR line %d\n", __LINE__); return -1; }\
} while (0)
/*----------------------------------------------------------------------
| constants
+---------------------------------------------------------------------*/
#define BANNER "Fragment Parser Test - Version 1.0\n"\
"(Bento4 Version " AP4_VERSION_STRING ")\n"\
"(c) 2002-2009 Axiomatic Systems, LLC"
/*----------------------------------------------------------------------
| PrintUsageAndExit
+---------------------------------------------------------------------*/
static void
PrintUsageAndExit()
{
fprintf(stderr,
BANNER
"\n\nusage: fragmentparsertest <test-filename>\n");
exit(1);
}
/*----------------------------------------------------------------------
| ShowSample
+---------------------------------------------------------------------*/
static void
ShowSample(AP4_Sample& sample, unsigned int index, AP4_SampleDecrypter* sample_decrypter)
{
printf("[%06d] size=%6d duration=%6d",
index,
(int)sample.GetSize(),
(int)sample.GetDuration());
printf(" offset=%10lld dts=%10lld cts=%10lld ",
sample.GetOffset(),
sample.GetDts(),
sample.GetCts());
if (sample.IsSync()) {
printf(" [S] ");
} else {
printf(" ");
}
AP4_DataBuffer sample_data;
sample.ReadData(sample_data);
AP4_DataBuffer* data = &sample_data;
AP4_DataBuffer decrypted_sample_data;
if (sample_decrypter) {
sample_decrypter->DecryptSampleData(sample_data, decrypted_sample_data);
data = & decrypted_sample_data;
}
unsigned int show = data->GetDataSize();
if (show > 12) show = 12; // max first 12 chars
for (unsigned int i=0; i<show; i++) {
printf("%02x", data->GetData()[i]);
}
if (show == data->GetDataSize()) {
printf("\n");
} else {
printf("...\n");
}
}
/*----------------------------------------------------------------------
| ProcessSamples
+---------------------------------------------------------------------*/
static int
ProcessSamples(AP4_Track* track,
AP4_ContainerAtom* traf,
AP4_ByteStream& moof_data,
AP4_Position moof_data_offset,
AP4_FragmentSampleTable* sample_table)
{
// look at the first sample description
AP4_SampleDecrypter* sample_decrypter = NULL;
if (track) {
AP4_UI08 key_1[16] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
AP4_UI08 key_2[16] = {0xAA, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
AP4_UI08* key = NULL;
AP4_Size key_size = 16;
if (track->GetId() == 1) {
key = key_1;
} else {
key = key_2;
}
AP4_SampleDescription* sample_description = track->GetSampleDescription(0);
if (sample_description->GetType() == AP4_SampleDescription::TYPE_PROTECTED) {
AP4_ProtectedSampleDescription* prot_desc = AP4_DYNAMIC_CAST(AP4_ProtectedSampleDescription, sample_description);
sample_decrypter = AP4_SampleDecrypter::Create(prot_desc,
traf,
moof_data,
moof_data_offset,
key,
key_size,
NULL);
}
}
printf("found %d samples\n", sample_table->GetSampleCount());
for (unsigned int i=0; i<sample_table->GetSampleCount(); i++) {
AP4_Sample sample;
AP4_Result result = sample_table->GetSample(i, sample);
CHECK(AP4_SUCCEEDED(result));
ShowSample(sample, i, sample_decrypter);
}
return 0;
}
/*----------------------------------------------------------------------
| ProcessMoof
+---------------------------------------------------------------------*/
static int
ProcessMoof(AP4_Movie* movie,
AP4_ContainerAtom* moof,
AP4_ByteStream* sample_stream,
AP4_Position moof_offset,
AP4_Position mdat_payload_offset)
{
AP4_Result result;
AP4_MovieFragment* fragment = new AP4_MovieFragment(moof);
printf("fragment sequence number=%d\n", fragment->GetSequenceNumber());
AP4_FragmentSampleTable* sample_table = NULL;
// get all track IDs in this fragment
AP4_Array<AP4_UI32> ids;
fragment->GetTrackIds(ids);
printf("Found %d tracks in fragment: ", ids.ItemCount());
for (unsigned int i=0; i<ids.ItemCount(); i++) {
printf("%d ", ids[i]);
}
printf("\n");
for (unsigned int i=0; i<ids.ItemCount(); i++) {
AP4_Track* track = NULL;
if (movie) {
track = movie->GetTrack(ids[i]);
}
AP4_ContainerAtom* traf = NULL;
fragment->GetTrafAtom(ids[i], traf);
printf("processing moof for track id %d\n", ids[i]);
result = fragment->CreateSampleTable(movie, ids[i], sample_stream, moof_offset, mdat_payload_offset, 0, sample_table);
CHECK(result == AP4_SUCCESS || result == AP4_ERROR_NO_SUCH_ITEM);
if (AP4_SUCCEEDED(result) ) {
ProcessSamples(track, traf, *sample_stream, moof_offset, sample_table);
delete sample_table;
} else {
printf("no sample table for this track\n");
}
}
delete fragment;
return 0;
}
/*----------------------------------------------------------------------
| main
+---------------------------------------------------------------------*/
int
main(int argc, char** argv)
{
if (argc != 2) {
PrintUsageAndExit();
}
const char* input_filename = argv[1];
// open the input
AP4_ByteStream* input = NULL;
AP4_Result result = AP4_FileByteStream::Create(input_filename, AP4_FileByteStream::STREAM_MODE_READ, input);
if (AP4_FAILED(result)) {
fprintf(stderr, "ERROR: cannot open input file (%s)\n", input_filename);
return 1;
}
// get the movie
AP4_File* file = new AP4_File(*input, true);
AP4_Movie* movie = file->GetMovie();
AP4_Atom* atom = NULL;
AP4_DefaultAtomFactory atom_factory;
do {
// process the next atom
result = atom_factory.CreateAtomFromStream(*input, atom);
if (AP4_SUCCEEDED(result)) {
printf("atom size=%lld\n", atom->GetSize());
if (atom->GetType() == AP4_ATOM_TYPE_MOOF) {
AP4_ContainerAtom* moof = AP4_DYNAMIC_CAST(AP4_ContainerAtom, atom);
if (moof) {
// remember where we are in the stream
AP4_Position position = 0;
input->Tell(position);
// process the movie fragment
ProcessMoof(movie, moof, input, position-atom->GetSize(), position+8);
// go back to where we were before processing the fragment
input->Seek(position);
}
} else {
delete atom;
}
}
} while (AP4_SUCCEEDED(result));
// cleanup
delete file;
input->Release();
return 0;
}
|