Debian Codegraph @

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

raw
1 /*****************************************************************
2 |
3 | AP4 - MP4 File Processor
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 "MP4 File Editor - Version 1.2\n"\
41 "(Bento4 Version " AP4_VERSION_STRING ")\n"\
42 "(c) 2002-2017 Axiomatic Systems, LLC"
43
44 const AP4_LargeSize AP4_MP4EDIT_MAX_PAYLOAD_SIZE = (AP4_LargeSize)0xFFFFFFFFUL;
45
46 /*----------------------------------------------------------------------
47 | PrintUsageAndExit
48 +---------------------------------------------------------------------*/
49 static void
50 PrintUsageAndExit()
51 {
52 fprintf(stderr,
53 BANNER
54 "\n\nusage: mp4edit [commands] <input> <output>\n"
55 " where commands include one or more of:\n"
56 " --insert <atom_path>:<atom_source>[:<position>]\n"
57 " --remove <atom_path>\n"
58 " --replace <atom_path>:<atom_source>\n"
59 "\n"
60 " and <atom_source> may be either a filename for a file\n"
61 " that contains the atom data (header and payload), or\n"
62 " <uuid>#<file> with <uuid> specifying an atom uuid type,\n"
63 " as a 32-character hex value, and <file> a file with the atom\n"
64 " payload only.\n");
65 exit(1);
66 }
67
68 /*----------------------------------------------------------------------
69 | AP4_EditingProcessor
70 +---------------------------------------------------------------------*/
71 class AP4_EditingProcessor : public AP4_Processor
72 {
73 public:
74 // types
75 class Command {
76 public:
77 // types
78 typedef enum {
79 TYPE_INSERT,
80 TYPE_REMOVE,
81 TYPE_REPLACE
82 } Type;
83
84 // constructor
85 Command(Type type, const char* atom_path,
86 const char* atom_source,
87 int position = -1) :
88 m_Type(type),
89 m_AtomPath(atom_path),
90 m_AtomSource(atom_source),
91 m_Position(position) {}
92
93 // members
94 Type m_Type;
95 AP4_String m_AtomPath;
96 AP4_String m_AtomSource;
97 int m_Position;
98 };
99
100 // constructor and destructor
101 virtual ~AP4_EditingProcessor();
102
103 // methods
104 virtual AP4_Result Initialize(AP4_AtomParent& top_level,
105 AP4_ByteStream& stream,
106 ProgressListener* listener);
107 AP4_Result AddCommand(Command::Type type,
108 const char* atom_path,
109 const char* file_path,
110 int position = -1);
111
112 private:
113 // methods
114 AP4_Result InsertAtom(AP4_String& atom_source,
115 AP4_AtomParent* container,
116 int position);
117 AP4_Result DoRemove(Command* command, AP4_AtomParent& top_level);
118 AP4_Result DoInsert(Command* command, AP4_AtomParent& top_level);
119 AP4_Result DoReplace(Command* command, AP4_AtomParent& top_level);
120
121 // members
122 AP4_List<Command> m_Commands;
123 AP4_AtomParent m_TopLevelParent;
124 };
125
126 /*----------------------------------------------------------------------
127 | AP4_EditingProcessor::~AP4_EditingProcessor
128 +---------------------------------------------------------------------*/
129 AP4_EditingProcessor::~AP4_EditingProcessor()
130 {
131 m_Commands.DeleteReferences();
132 }
133
134 /*----------------------------------------------------------------------
135 | AP4_EditingProcessor::AddCommand
136 +---------------------------------------------------------------------*/
137 AP4_Result
138 AP4_EditingProcessor::AddCommand(Command::Type type,
139 const char* atom_path,
140 const char* file_path,
141 int position)
142 {
143 return m_Commands.Add(new Command(type, atom_path, file_path, position));
144 }
145
146 /*----------------------------------------------------------------------
147 | AP4_EditingProcessor::Initialize
148 +---------------------------------------------------------------------*/
149 AP4_Result
150 AP4_EditingProcessor::Initialize(AP4_AtomParent& top_level,
151 AP4_ByteStream&,
152 ProgressListener*)
153 {
154 AP4_Result result;
155
156 AP4_List<Command>::Item* command_item = m_Commands.FirstItem();
157 while (command_item) {
158 Command* command = command_item->GetData();
159 switch (command->m_Type) {
160 case Command::TYPE_REMOVE:
161 result = DoRemove(command, top_level);
162 if (AP4_FAILED(result)) return result;
163 break;
164
165 case Command::TYPE_INSERT:
166 result = DoInsert(command, top_level);
167 if (AP4_FAILED(result)) return result;
168 break;
169
170 case Command::TYPE_REPLACE:
171 result = DoReplace(command, top_level);
172 if (AP4_FAILED(result)) return result;
173 break;
174 }
175 command_item = command_item->GetNext();
176 }
177
178 return AP4_SUCCESS;
179 }
180
181 /*----------------------------------------------------------------------
182 | AP4_EditingProcessor::DoRemove
183 +---------------------------------------------------------------------*/
184 AP4_Result
185 AP4_EditingProcessor::DoRemove(Command* command, AP4_AtomParent& top_level)
186 {
187 AP4_Atom* atom = top_level.FindChild(command->m_AtomPath.GetChars());
188 if (atom == NULL) {
189 fprintf(stderr, "ERROR: atom '%s' not found\n", command->m_AtomPath.GetChars());
190 return AP4_FAILURE;
191 } else {
192 atom->Detach();
193 delete atom;
194 return AP4_SUCCESS;
195 }
196 }
197
198 /*----------------------------------------------------------------------
199 | AP4_EditingProcessor::InsertAtom
200 +---------------------------------------------------------------------*/
201 AP4_Result
202 AP4_EditingProcessor::InsertAtom(AP4_String& atom_source,
203 AP4_AtomParent* container,
204 int position)
205 {
206 const char* file_path = atom_source.GetChars();
207 int separator = atom_source.Find('#');
208 AP4_UI08 atom_uuid[16];
209 bool is_uuid = false;
210 if (separator == 32) {
211 // uuid
212 AP4_ParseHex(atom_source.GetChars(), atom_uuid, 16);
213 file_path += separator + 1;
214 is_uuid = true;
215 }
216
217 AP4_Atom* child = NULL;
218 if (is_uuid) {
219 // open the payload
220 AP4_ByteStream* payload = NULL;
221 AP4_Result result = AP4_FileByteStream::Create(file_path, AP4_FileByteStream::STREAM_MODE_READ, payload);
222 if (AP4_FAILED(result)) {
223 fprintf(stderr, "ERROR: cannot open atom file (%s)\n", file_path);
224 return result;
225 }
226
227 // check the size
228 AP4_LargeSize payload_size = 0;
229 result = payload->GetSize(payload_size);
230 if (AP4_FAILED(result)) {
231 fprintf(stderr, "ERROR: cannot get atom file size\n");
232 payload->Release();
233 return result;
234 }
235 if (payload_size > AP4_MP4EDIT_MAX_PAYLOAD_SIZE) {
236 fprintf(stderr, "ERROR: atom payload too large\n");
237 payload->Release();
238 return AP4_ERROR_OUT_OF_RANGE;
239 }
240
241 // synthesize a uuid atom
242 child = new AP4_UnknownUuidAtom(payload_size + AP4_ATOM_HEADER_SIZE + 16, atom_uuid, *payload);
243 payload->Release();
244 } else {
245 // read the atom to insert
246 AP4_ByteStream* input = NULL;
247 AP4_Result result = AP4_FileByteStream::Create(file_path, AP4_FileByteStream::STREAM_MODE_READ, input);
248 if (AP4_FAILED(result)) {
249 fprintf(stderr, "ERROR: cannot open atom file (%s)\n", file_path);
250 return AP4_FAILURE;
251 }
252
253 AP4_DefaultAtomFactory atom_factory;
254 result = atom_factory.CreateAtomFromStream(*input, child);
255 input->Release();
256 if (AP4_FAILED(result)) {
257 fprintf(stderr, "ERROR: failed to create atom\n");
258 return AP4_FAILURE;
259 }
260 }
261
262 // insert the atom
263 AP4_Result result = container->AddChild(child, position);
264 if (AP4_FAILED(result)) {
265 fprintf(stderr, "ERROR: failed to insert atom\n");
266 delete child;
267 return result;
268 }
269
270 return AP4_SUCCESS;
271 }
272
273 /*----------------------------------------------------------------------
274 | AP4_EditingProcessor::DoInsert
275 +---------------------------------------------------------------------*/
276 AP4_Result
277 AP4_EditingProcessor::DoInsert(Command* command, AP4_AtomParent& top_level)
278 {
279 AP4_AtomParent* parent = NULL;
280 if (command->m_AtomPath.GetLength() == 0) {
281 // insert into the toplevel list
282 parent = &top_level;
283 } else {
284 // find the atom to insert into
285 AP4_Atom* atom = top_level.FindChild(command->m_AtomPath.GetChars(), true);
286 if (atom == NULL) {
287 fprintf(stderr, "ERROR: atom '%s' not found\n",
288 command->m_AtomPath.GetChars());
289 return AP4_FAILURE;
290 }
291
292 // check that the atom is a container
293 parent = AP4_DYNAMIC_CAST(AP4_AtomParent, atom);
294 }
295
296 // check that we have a place to insert into
297 if (parent == NULL) {
298 fprintf(stderr, "ERROR: atom '%s' is not a container\n",
299 command->m_AtomPath.GetChars());
300 return AP4_FAILURE;
301 }
302
303 return InsertAtom(command->m_AtomSource, parent, command->m_Position);
304 }
305
306 /*----------------------------------------------------------------------
307 | AP4_EditingProcessor::DoReplace
308 +---------------------------------------------------------------------*/
309 AP4_Result
310 AP4_EditingProcessor::DoReplace(Command* command, AP4_AtomParent& top_level)
311 {
312 // remove the atom
313 AP4_Atom* atom = top_level.FindChild(command->m_AtomPath.GetChars());
314 if (atom == NULL) {
315 fprintf(stderr, "ERROR: atom '%s' not found\n", command->m_AtomPath.GetChars());
316 return AP4_FAILURE;
317 } else {
318 // find the position of the atom in the parent
319 AP4_AtomParent* parent = atom->GetParent();
320 int position = 0;
321 AP4_List<AP4_Atom>::Item* list_item = parent->GetChildren().FirstItem();
322 while (list_item) {
323 if (list_item->GetData() == atom) break;
324 position++;
325 list_item = list_item->GetNext();
326 }
327
328 // remove the atom from the parent
329 atom->Detach();
330 delete atom;
331
332 // insert the replacement
333 return InsertAtom(command->m_AtomSource, parent, position);
334 }
335 }
336
337 /*----------------------------------------------------------------------
338 | main
339 +---------------------------------------------------------------------*/
340 int
341 main(int argc, char** argv)
342 {
343 if (argc < 3) {
344 PrintUsageAndExit();
345 }
346
347 // create initial objects
348 AP4_EditingProcessor processor;
349
350 // parse arguments
351 const char* input_filename = NULL;
352 const char* output_filename = NULL;
353 char* arg;
354 while ((arg = *++argv)) {
355 if (!AP4_CompareStrings(arg, "--insert")) {
356 char* param = *++argv;
357 if (param == NULL) {
358 fprintf(stderr, "ERROR: missing argument for --insert command\n");
359 return 1;
360 }
361 char* atom_path = NULL;
362 char* file_path = NULL;
363 if (AP4_SUCCEEDED(AP4_SplitArgs(param, atom_path, file_path))) {
364 if (file_path[0] == '\0') {
365 // empty file path
366 fprintf(stderr, "ERROR: missing file path in --insert argument\n");
367 return 1;
368 }
369 bool is_windows_path = false;
370 if (((file_path[0] >= 'a' && file_path[0] <= 'z') ||
371 (file_path[0] >= 'A' && file_path[0] <= 'Z')) &&
372 file_path[1] == ':' && file_path[2] == '\\') {
373 is_windows_path = true;
374 file_path[1] = '@'; // temporary replacement so that the next split works
375 }
376 int position = -1;
377 char* position_str = NULL;
378 if (AP4_SUCCEEDED(AP4_SplitArgs(file_path, file_path, position_str))) {
379 if (position_str) {
380 position = (int)strtoul(position_str, NULL, 10);
381 }
382 }
383 if (is_windows_path) {
384 file_path[1] = ':';
385 }
386 processor.AddCommand(AP4_EditingProcessor::Command::TYPE_INSERT, atom_path, file_path, position);
387 } else {
388 fprintf(stderr, "ERROR: invalid format for --insert command argument\n");
389 return 1;
390 }
391 } else if (!AP4_CompareStrings(arg, "--remove")) {
392 char* atom_path = *++argv;
393 if (atom_path == NULL) {
394 fprintf(stderr, "ERROR: missing argument for --remove command\n");
395 return 1;
396 }
397 processor.AddCommand(AP4_EditingProcessor::Command::TYPE_REMOVE, atom_path, "");
398 } else if (!AP4_CompareStrings(arg, "--replace")) {
399 char* param = *++argv;
400 if (param == NULL) {
401 fprintf(stderr, "ERROR: missing argument for --replace command\n");
402 return 1;
403 }
404 char* atom_path = NULL;
405 char* file_path = NULL;
406 if (AP4_SUCCEEDED(AP4_SplitArgs(param, atom_path, file_path))) {
407 processor.AddCommand(AP4_EditingProcessor::Command::TYPE_REPLACE, atom_path, file_path);
408 } else {
409 fprintf(stderr, "ERROR: invalid format for --replace command argument\n");
410 return 1;
411 }
412 } else if (input_filename == NULL) {
413 input_filename = arg;
414 } else if (output_filename == NULL) {
415 output_filename = arg;
416 } else {
417 fprintf(stderr, "ERROR: invalid command line argument (%s)\n", arg);
418 return 1;
419 }
420 }
421
422 // check arguments
423 if (input_filename == NULL) {
424 fprintf(stderr, "ERROR: missing input filename\n");
425 return 1;
426 }
427 if (output_filename == NULL) {
428 fprintf(stderr, "ERROR: missing output filename\n");
429 return 1;
430 }
431
432 // create the input stream
433 AP4_Result result;
434 AP4_ByteStream* input = NULL;
435 result = AP4_FileByteStream::Create(input_filename, AP4_FileByteStream::STREAM_MODE_READ, input);
436 if (AP4_FAILED(result)) {
437 fprintf(stderr, "ERROR: cannot open input file (%s)\n", input_filename);
438 return 1;
439 }
440
441 // create the output stream
442 AP4_ByteStream* output = NULL;
443 result = AP4_FileByteStream::Create(output_filename, AP4_FileByteStream::STREAM_MODE_WRITE, output);
444 if (AP4_FAILED(result)) {
445 fprintf(stderr, "ERROR: cannot open output file (%s)\n", output_filename);
446 input->Release();
447 return 1;
448 }
449
450 // process!
451 processor.Process(*input, *output);
452
453 // cleanup
454 input->Release();
455 output->Release();
456
457 return 0;
458 }
459