pre-release commit
[asdcplib.git] / src / klvwalk.cpp
1 /*
2 Copyright (c) 2005-2006, John Hurst
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8 1. Redistributions of source code must retain the above copyright
9    notice, this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11    notice, this list of conditions and the following disclaimer in the
12    documentation and/or other materials provided with the distribution.
13 3. The name of the author may not be used to endorse or promote products
14    derived from this software without specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 /*! \file    klvwalk.cpp
28     \version $Id$
29     \brief   KLV+MXF test
30 */
31
32 #include <AS_DCP.h>
33 #include <MXF.h>
34 #include <hex_utils.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <ctype.h>
38 #include <assert.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41
42 using namespace ASDCP;
43
44
45 //------------------------------------------------------------------------------------------
46 //
47
48 // There is no header file thet defines this function.
49 // You just have to know it's there...
50 void set_debug_mode(bool info_mode, bool debug_mode);
51
52
53 int
54 main(int argc, char** argv)
55 {
56   Result_t result = RESULT_OK;
57   bool read_mxf = false;
58   bool rewrite_mxf = false;
59   int arg_i = 1;
60   set_debug_mode(true, true);
61
62   if ( strcmp(argv[1], "-r") == 0 )
63     {
64       read_mxf = true;
65       arg_i++;
66     }
67   else if ( strcmp(argv[1], "-w") == 0 )
68     {
69       rewrite_mxf = true;
70       arg_i++;
71       assert(argc - arg_i == 2);
72     }
73
74   fprintf(stderr, "Opening file %s\n", argv[arg_i]);
75
76   if ( read_mxf )
77     {
78       ASDCP::FileReader        Reader;
79       ASDCP::MXF::OPAtomHeader Header;
80
81       result = Reader.OpenRead(argv[arg_i]);
82
83       if ( ASDCP_SUCCESS(result) )
84         result = Header.InitFromFile(Reader);
85
86       //      if ( ASDCP_SUCCESS(result) )
87       Header.Dump();
88
89       if ( ASDCP_SUCCESS(result) )
90         {
91           ASDCP::MXF::OPAtomIndexFooter Index;
92           result = Reader.Seek(Header.FooterPartition);
93
94           if ( ASDCP_SUCCESS(result) )
95             {
96               Index.m_Lookup = &Header.m_Primer;
97               result = Index.InitFromFile(Reader);
98             }
99
100           if ( ASDCP_SUCCESS(result) )
101             Index.Dump();
102         }
103     }
104   else if ( rewrite_mxf )
105     {
106       ASDCP::FileReader        Reader;
107       ASDCP::FileWriter        Writer;
108       ASDCP::MXF::OPAtomHeader Header;
109       ASDCP::MXF::OPAtomIndexFooter Index;
110
111       result = Reader.OpenRead(argv[arg_i++]);
112
113       if ( ASDCP_SUCCESS(result) )
114         result = Header.InitFromFile(Reader);
115
116       if ( ASDCP_SUCCESS(result) )
117         result = Reader.Seek(Header.FooterPartition);
118
119       if ( ASDCP_SUCCESS(result) )
120         result = Index.InitFromFile(Reader);
121
122       Header.m_Primer.ClearTagList();
123
124       if ( ASDCP_SUCCESS(result) )
125         result = Writer.OpenWrite(argv[arg_i]);
126
127       if ( ASDCP_SUCCESS(result) )
128         result = Header.WriteToFile(Writer);
129
130 //      if ( ASDCP_SUCCESS(result) )
131 //      result = Index.WriteToFile(Writer);
132
133       // essence packets
134
135       // index
136
137       // RIP
138     }
139   else // dump klv
140     {
141       ASDCP::FileReader Reader;
142       KLVFilePacket KP;
143
144       result = Reader.OpenRead(argv[arg_i]);
145
146       if ( ASDCP_SUCCESS(result) )
147         result = KP.InitFromFile(Reader);
148
149       while ( ASDCP_SUCCESS(result) )
150         {
151           KP.Dump(stderr, true);
152           result = KP.InitFromFile(Reader);
153         }
154
155       if( result == RESULT_ENDOFFILE )
156         result = RESULT_OK;
157     }
158
159   if ( result != RESULT_OK )
160     {
161       fputs("Program stopped on error.\n", stderr);
162
163       if ( result != RESULT_FAIL )
164         {
165           fputs(GetResultString(result), stderr);
166           fputc('\n', stderr);
167         }
168
169       return 1;
170     }
171
172   return 0;
173 }
174
175
176 //
177 // end klvwalk.cpp
178 //
179
180
181
182
183
184
185
186
187
188