| 1 |
/***************************************************************** |
| 2 |
| |
| 3 |
| AP4 - Passthrough Writer Test |
| 4 |
| |
| 5 |
| Copyright 2002-2008 Axiomatic Systems, LLC |
| 6 |
| |
| 7 |
| |
| 8 |
| This file is part of Bento4/AP4 (MP4 Atom Processing Library). |
| 9 |
| |
| 10 |
| Unless you have obtained Bento4 under a difference license, |
| 11 |
| this version of Bento4 is Bento4|GPL. |
| 12 |
| Bento4|GPL is free software; you can redistribute it and/or modify |
| 13 |
| it under the terms of the GNU General Public License as published by |
| 14 |
| the Free Software Foundation; either version 2, or (at your option) |
| 15 |
| any later version. |
| 16 |
| |
| 17 |
| Bento4|GPL is distributed in the hope that it will be useful, |
| 18 |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 |
| GNU General Public License for more details. |
| 21 |
| |
| 22 |
| You should have received a copy of the GNU General Public License |
| 23 |
| along with Bento4|GPL; see the file COPYING. If not, write to the |
| 24 |
| Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
| 25 |
| 02111-1307, USA. |
| 26 |
| |
| 27 |
****************************************************************/ |
| 28 |
|
| 29 |
/*---------------------------------------------------------------------- |
| 30 |
| includes |
| 31 |
+---------------------------------------------------------------------*/ |
| 32 |
#include <stdio.h> |
| 33 |
#include <stdlib.h> |
| 34 |
|
| 35 |
#include "Ap4.h" |
| 36 |
|
| 37 |
/*---------------------------------------------------------------------- |
| 38 |
| constants |
| 39 |
+---------------------------------------------------------------------*/ |
| 40 |
#define BANNER "Passthrough Writer Test - Version 1.0\n"\ |
| 41 |
"(Bento4 Version " AP4_VERSION_STRING ")\n"\ |
| 42 |
"(c) 2002-2008 Axiomatic Systems, LLC" |
| 43 |
|
| 44 |
/*---------------------------------------------------------------------- |
| 45 |
| PrintUsageAndExit |
| 46 |
+---------------------------------------------------------------------*/ |
| 47 |
static void |
| 48 |
PrintUsageAndExit() |
| 49 |
{ |
| 50 |
fprintf(stderr, |
| 51 |
BANNER |
| 52 |
"\n\nusage: passthroughwritertest [options] <input> <output>\n"); |
| 53 |
exit(1); |
| 54 |
} |
| 55 |
|
| 56 |
/*---------------------------------------------------------------------- |
| 57 |
| CopyTrack |
| 58 |
+---------------------------------------------------------------------*/ |
| 59 |
static void |
| 60 |
CopyTrack(AP4_Movie* input_movie, AP4_Track* input_track, AP4_Movie* output_movie) |
| 61 |
{ |
| 62 |
AP4_SyntheticSampleTable* sample_table = new AP4_SyntheticSampleTable(); |
| 63 |
AP4_SampleDescription* sample_description = input_track->GetSampleDescription(0); |
| 64 |
|
| 65 |
#if 0 |
| 66 |
if (input_track->GetType() == AP4_Track::TYPE_VIDEO) { |
| 67 |
AP4_ContainerAtom* schi = new AP4_ContainerAtom(AP4_ATOM_TYPE_SCHI); |
| 68 |
sample_description = new AP4_ProtectedSampleDescription( |
| 69 |
AP4_ATOM_TYPE_ENCV, |
| 70 |
sample_description, |
| 71 |
sample_description->GetFormat(), |
| 72 |
AP4_PROTECTION_SCHEME_TYPE_OMA, |
| 73 |
AP4_PROTECTION_SCHEME_VERSION_OMA_20, |
| 74 |
"http://foo.bar/prot", |
| 75 |
schi, |
| 76 |
false); |
| 77 |
delete schi; |
| 78 |
} |
| 79 |
#endif |
| 80 |
|
| 81 |
// add the description to the table without transfering ownership |
| 82 |
sample_table->AddSampleDescription(sample_description, false); |
| 83 |
|
| 84 |
AP4_Sample sample; |
| 85 |
AP4_Ordinal index = 0; |
| 86 |
printf("Copying track %d\n", input_track->GetId()); |
| 87 |
while (AP4_SUCCEEDED(input_track->GetSample(index, sample))) { |
| 88 |
if (sample.GetDescriptionIndex() != 0) { |
| 89 |
// just for the test, fix that later... too lazy for now.... |
| 90 |
fprintf(stderr, "ERROR: unable to write tracks with more than one sample desc"); |
| 91 |
} |
| 92 |
|
| 93 |
AP4_ByteStream* data_stream; |
| 94 |
data_stream = sample.GetDataStream(); |
| 95 |
sample_table->AddSample(*data_stream, |
| 96 |
sample.GetOffset(), |
| 97 |
sample.GetSize(), |
| 98 |
sample.GetDuration(), |
| 99 |
sample.GetDescriptionIndex(), |
| 100 |
sample.GetDts(), |
| 101 |
sample.GetCtsDelta(), |
| 102 |
sample.IsSync()); |
| 103 |
AP4_RELEASE(data_stream); // release our ref, the table has kept its own ref. |
| 104 |
index++; |
| 105 |
} |
| 106 |
printf("Copied %d samples\n", index); |
| 107 |
|
| 108 |
// create an output track |
| 109 |
AP4_Track* output_track = new AP4_Track(input_track->GetType(), |
| 110 |
sample_table, |
| 111 |
input_track->GetId(), |
| 112 |
input_movie->GetTimeScale(), |
| 113 |
input_track->GetDuration(), |
| 114 |
input_track->GetMediaTimeScale(), |
| 115 |
input_track->GetMediaDuration(), |
| 116 |
input_track->GetTrackLanguage(), |
| 117 |
input_track->GetWidth(), |
| 118 |
input_track->GetHeight()); |
| 119 |
|
| 120 |
// add the track to the output movie |
| 121 |
output_movie->AddTrack(output_track); |
| 122 |
} |
| 123 |
|
| 124 |
/*---------------------------------------------------------------------- |
| 125 |
| main |
| 126 |
+---------------------------------------------------------------------*/ |
| 127 |
int |
| 128 |
main(int argc, char** argv) |
| 129 |
{ |
| 130 |
if (argc != 3) { |
| 131 |
PrintUsageAndExit(); |
| 132 |
} |
| 133 |
const char* input_filename = argv[1]; |
| 134 |
const char* output_filename = argv[2]; |
| 135 |
|
| 136 |
// open the input |
| 137 |
AP4_Result result; |
| 138 |
AP4_ByteStream* input = NULL; |
| 139 |
result = AP4_FileByteStream::Create(input_filename, AP4_FileByteStream::STREAM_MODE_READ, input); |
| 140 |
if (AP4_FAILED(result)) { |
| 141 |
fprintf(stderr, "ERROR: cannot open input file (%s)\n", input_filename); |
| 142 |
return 1; |
| 143 |
} |
| 144 |
|
| 145 |
// open the output |
| 146 |
AP4_ByteStream* output = NULL; |
| 147 |
result = AP4_FileByteStream::Create(output_filename, AP4_FileByteStream::STREAM_MODE_WRITE, output); |
| 148 |
if (AP4_FAILED(result)) { |
| 149 |
fprintf(stderr, "ERROR: cannot open output file (%s)\n", output_filename); |
| 150 |
return 1; |
| 151 |
} |
| 152 |
|
| 153 |
// get the movie |
| 154 |
AP4_File* input_file = new AP4_File(*input); |
| 155 |
AP4_Movie* input_movie = input_file->GetMovie(); |
| 156 |
if (input_movie == NULL) { |
| 157 |
fprintf(stderr, "ERROR: no movie in file\n"); |
| 158 |
return 1; |
| 159 |
} |
| 160 |
|
| 161 |
AP4_Movie* output_movie = new AP4_Movie(input_movie->GetTimeScale()); |
| 162 |
AP4_File* output_file = new AP4_File(output_movie); |
| 163 |
output_file->SetFileType(input_file->GetFileType()->GetMajorBrand(), |
| 164 |
input_file->GetFileType()->GetMinorVersion(), |
| 165 |
&input_file->GetFileType()->GetCompatibleBrands()[0], |
| 166 |
input_file->GetFileType()->GetCompatibleBrands().ItemCount()); |
| 167 |
|
| 168 |
// copy all tracks |
| 169 |
AP4_List<AP4_Track>& tracks = input_movie->GetTracks(); |
| 170 |
AP4_List<AP4_Track>::Item* track_item = tracks.FirstItem(); |
| 171 |
while (track_item) { |
| 172 |
AP4_Track* track = track_item->GetData(); |
| 173 |
CopyTrack(input_movie, track, output_movie); |
| 174 |
track_item = track_item->GetNext(); |
| 175 |
} |
| 176 |
|
| 177 |
// write the file to the output |
| 178 |
AP4_FileWriter::Write(*output_file, *output); |
| 179 |
|
| 180 |
// cleanup |
| 181 |
delete input_file; |
| 182 |
delete output_file; |
| 183 |
input->Release(); |
| 184 |
output->Release(); |
| 185 |
|
| 186 |
return 0; |
| 187 |
} |
| 188 |
|