Debian Codegraph @

bento4-embedded/Source/C++/Apps/Mp4IframeIndex/Mp4IframeIndex.cpp

raw
1 /*****************************************************************
2 |
3 | AP4 - MP4 I-Frame Indexer
4 |
5 | Copyright 2002-2017 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 "MP4 Iframe Index - Version 1.0.0\n"\
41 "(Bento4 Version " AP4_VERSION_STRING ")\n"\
42 "(c) 2002-2017 Axiomatic Systems, LLC"
43
44 /*----------------------------------------------------------------------
45 | PrintUsageAndExit
46 +---------------------------------------------------------------------*/
47 static void
48 PrintUsageAndExit()
49 {
50 fprintf(stderr,
51 BANNER
52 "\n\nusage: mp4iframeindex [options] <input> [<output>]\n"
53 " options:\n"
54 " --track <id>: ID of the video track\n"
55 );
56 exit(1);
57 }
58
59 /*----------------------------------------------------------------------
60 | IndexTrack
61 +---------------------------------------------------------------------*/
62 static AP4_Result
63 IndexTrack(AP4_Track& track, const char** separator, AP4_ByteStream* output)
64 {
65 AP4_Sample sample;
66 for (unsigned int i=0; i<track.GetSampleCount(); i++) {
67 // get the sample
68 AP4_Result result = track.GetSample(i, sample);
69 if (AP4_FAILED(result)) return result;
70
71 if (sample.IsSync()) {
72 AP4_Offset offset = sample.GetOffset();
73 char workspace[256];
74 AP4_FormatString(workspace, sizeof(workspace),
75 "%s{ \"size\": %u, \"offset\": %llu, \"fragmentStart\": 0 }",
76 *separator,
77 (unsigned int)sample.GetSize(),
78 (unsigned long long)offset);
79 *separator = ",\n";
80 output->WriteString(workspace);
81 }
82 }
83
84 return AP4_SUCCESS;
85 }
86
87 /*----------------------------------------------------------------------
88 | IndexFragments
89 +---------------------------------------------------------------------*/
90 static AP4_Result
91 IndexFragments(AP4_Movie& movie, unsigned int track_id, AP4_ByteStream* stream, const char** separator, AP4_ByteStream* output)
92 {
93 stream->Seek(0);
94 AP4_LinearReader reader(movie, stream);
95 reader.EnableTrack(track_id);
96
97 AP4_Sample sample;
98 AP4_Position last_fragment_position = (AP4_Position)(-1);
99 for (unsigned int i=0; ; i++) {
100 AP4_UI32 found_track_id = 0;
101 AP4_Result result = reader.GetNextSample(sample, found_track_id);
102 if (AP4_FAILED(result)) break;
103
104 // only output the first sync sample of each fragment
105 if (reader.GetCurrentFragmentPosition() == last_fragment_position) {
106 continue;
107 }
108 if (found_track_id == track_id && sample.IsSync()) {
109 AP4_Offset offset = sample.GetOffset();
110 char workspace[256];
111 AP4_FormatString(workspace, sizeof(workspace),
112 "%s{ \"size\": %u, \"offset\": %llu, \"fragmentStart\": %llu }",
113 *separator,
114 (unsigned int)sample.GetSize(),
115 (unsigned long long)offset,
116 (unsigned long long)reader.GetCurrentFragmentPosition());
117 *separator = ",\n";
118 output->WriteString(workspace);
119 }
120 last_fragment_position = reader.GetCurrentFragmentPosition();
121 }
122
123 return AP4_SUCCESS;
124 }
125
126 /*----------------------------------------------------------------------
127 | main
128 +---------------------------------------------------------------------*/
129 int
130 main(int argc, char** argv)
131 {
132 if (argc < 2) {
133 PrintUsageAndExit();
134 }
135 const char* input_filename = NULL;
136 const char* output_filename = NULL;
137 const char* moov_filename = NULL;
138 unsigned int track_id = 0;
139
140 ++argv;
141 while (char* arg = *argv++) {
142 if (!strcmp(arg, "--track")) {
143 arg = *argv++;
144 if (arg == NULL) {
145 fprintf(stderr, "ERROR: missing argument after --track option\n");
146 return 1;
147 }
148 track_id = (unsigned int)strtoul(arg, NULL, 10);
149 } else if (!strcmp(arg, "--fragments-info")) {
150 moov_filename = *argv++;
151 if (moov_filename == NULL) {
152 fprintf(stderr, "ERROR: missing argument after --fragments-info option\n");
153 }
154 } else if (input_filename == NULL) {
155 input_filename = arg;
156 } else if (output_filename == NULL) {
157 output_filename = arg;
158 } else {
159 fprintf(stderr, "ERROR: unexpected argument '%s'\n", arg);
160 return 1;
161 }
162 }
163 if (input_filename == NULL) {
164 fprintf(stderr, "ERROR: input filename missing\n");
165 return 1;
166 }
167 if (output_filename == NULL) {
168 output_filename = "-stdout";
169 }
170
171 AP4_ByteStream* output = NULL;
172 AP4_Result result = AP4_FileByteStream::Create(output_filename,
173 AP4_FileByteStream::STREAM_MODE_WRITE,
174 output);
175 if (AP4_FAILED(result)) {
176 fprintf(stderr, "ERROR: cannot open output file %s (%d)\n", input_filename, result);
177 return 1;
178 }
179
180 AP4_ByteStream* input = NULL;
181 result = AP4_FileByteStream::Create(input_filename,
182 AP4_FileByteStream::STREAM_MODE_READ,
183 input);
184 if (AP4_FAILED(result)) {
185 fprintf(stderr, "ERROR: cannot open input file %s (%d)\n", input_filename, result);
186 return 1;
187 }
188
189 AP4_ByteStream* moov = NULL;
190 if (moov_filename) {
191 result = AP4_FileByteStream::Create(moov_filename,
192 AP4_FileByteStream::STREAM_MODE_READ,
193 moov);
194 if (AP4_FAILED(result)) {
195 fprintf(stderr, "ERROR: cannot open fragments info file %s (%d)\n", moov_filename, result);
196 return 1;
197 }
198 }
199
200 AP4_File* input_file = new AP4_File(*input, true);
201 AP4_Movie* input_movie = input_file->GetMovie();
202 AP4_File* moov_file = moov ? new AP4_File(*moov, true) : NULL;
203 AP4_Track* track = NULL;
204 const char* separator = "";
205
206 if (input_movie == NULL) {
207 // we need a movie to get to the fragments
208 if (moov_file == NULL) {
209 fprintf(stderr, "ERROR: input file does not contain a movie (use --fragments-info option)\n");
210 goto end;
211 }
212 input_movie = moov_file->GetMovie();
213 }
214
215 // select the track
216 if (track_id == 0) {
217 track = input_movie->GetTrack(AP4_Track::TYPE_VIDEO);
218 if (track) {
219 track_id = track->GetId();
220 }
221 } else {
222 track = input_movie->GetTrack(track_id);
223 }
224
225 // check that we found the track
226 if (!track) {
227 fprintf(stderr, "ERROR: video track not found\n");
228 goto end;
229 }
230
231 // check the track type
232 if (track->GetType() != AP4_Track::TYPE_VIDEO) {
233 fprintf(stderr, "ERROR: track ID %d is not a video track\n", track->GetId());
234 goto end;
235 }
236
237 // start
238 output->WriteString("[\n");
239
240 // index the track
241 result = IndexTrack(*track, &separator, output);
242 if (AP4_FAILED(result)) {
243 fprintf(stderr, "ERROR: failed to index track (%d)\n", result);
244 }
245
246 // index the fragments
247 result = IndexFragments(*input_movie, track_id, input, &separator, output);
248 if (AP4_FAILED(result)) {
249 fprintf(stderr, "ERROR: failed to index fragments (%d)\n", result);
250 }
251
252 // end
253 output->WriteString("\n]\n");
254
255 end:
256 delete input_file;
257 delete moov_file;
258 if (moov) moov->Release();
259 input->Release();
260 output->Release();
261
262 return result == AP4_SUCCESS ? 0 : 1;
263 }