fixed encryption for timed text
[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 <KM_log.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 using Kumu::DefaultLogSink;
44
45
46 //------------------------------------------------------------------------------------------
47 //
48 // command line option parser class
49
50 static const char* PACKAGE = "klvwalk";    // program name for messages
51 typedef std::list<std::string> FileList_t;
52
53 // Increment the iterator, test for an additional non-option command line argument.
54 // Causes the caller to return if there are no remaining arguments or if the next
55 // argument begins with '-'.
56 #define TEST_EXTRA_ARG(i,c)    if ( ++i >= argc || argv[(i)][0] == '-' ) \
57                                  { \
58                                    fprintf(stderr, "Argument not found for option -%c.\n", (c)); \
59                                    return; \
60                                  }
61
62 //
63 void
64 banner(FILE* stream = stdout)
65 {
66   fprintf(stream, "\n\
67 %s (asdcplib %s)\n\n\
68 Copyright (c) 2005-2006 John Hurst\n\
69 %s is part of the asdcplib DCP tools package.\n\
70 asdcplib may be copied only under the terms of the license found at\n\
71 the top of every file in the asdcplib distribution kit.\n\n\
72 Specify the -h (help) option for further information about %s\n\n",
73           PACKAGE, ASDCP::Version(), PACKAGE, PACKAGE);
74 }
75
76 //
77 void
78 usage(FILE* stream = stdout)
79 {
80   fprintf(stream, "\
81 USAGE: %s [-r] [-v] <input-file> [<input-file2> ...]\n\
82 \n\
83        %s [-h|-help] [-V]\n\
84 \n\
85   -h | -help  - Show help\n\
86   -r          - When KLV data is an OPAtom file, additionally\n\
87                 display OPAtom headers\n\
88   -v          - Verbose. Prints informative messages to stderr\n\
89   -V          - Show version information\n\
90 \n\
91   NOTES: o There is no option grouping, all options must be distinct arguments.\n\
92          o All option arguments must be separated from the option by whitespace.\n\
93 \n", PACKAGE, PACKAGE);
94 }
95
96 //
97 //
98  class CommandOptions
99  {
100    CommandOptions();
101
102  public:
103    bool   error_flag;               // true if the given options are in error or not complete
104    bool   version_flag;             // true if the version display option was selected
105    bool   help_flag;                // true if the help display option was selected
106    bool   verbose_flag;             // true if the informative messages option was selected
107    bool   read_mxf_flag;            // true if the -r option was selected
108    FileList_t inFileList;           // File to operate on
109
110    CommandOptions(int argc, const char** argv) :
111      error_flag(true), version_flag(false), help_flag(false), verbose_flag(false), read_mxf_flag(false)
112    {
113      for ( int i = 1; i < argc; i++ )
114        {
115
116          if ( (strcmp( argv[i], "-help") == 0) )
117            {
118              help_flag = true;
119              continue;
120            }
121          
122          if ( argv[i][0] == '-' && isalpha(argv[i][1]) && argv[i][2] == 0 )
123            {
124              switch ( argv[i][1] )
125                {
126
127                case 'h': help_flag = true; break;
128                case 'r': read_mxf_flag = true; break;
129                case 'V': version_flag = true; break;
130                case 'v': verbose_flag = true; break;
131
132                default:
133                  fprintf(stderr, "Unrecognized option: %s\n", argv[i]);
134                  return;
135                }
136            }
137          else
138            {
139              if ( argv[i][0] != '-' )
140                inFileList.push_back(argv[i]);
141
142              else
143                {
144                  fprintf(stderr, "Unrecognized option: %s\n", argv[i]);
145                  return;
146                }
147            }
148        }
149
150      if ( help_flag || version_flag )
151        return;
152      
153      if ( inFileList.empty() )
154        {
155          fputs("Input filename(s) required.\n", stderr);
156          return;
157        }
158      
159      error_flag = false;
160    }
161  };
162
163
164 //---------------------------------------------------------------------------------------------------
165 //
166
167 int
168 main(int argc, const char** argv)
169 {
170   CommandOptions Options(argc, argv);
171
172   if ( Options.version_flag )
173     banner();
174
175   if ( Options.help_flag )
176     usage();
177
178   if ( Options.version_flag || Options.help_flag )
179     return 0;
180
181   if ( Options.error_flag )
182     {
183       fprintf(stderr, "There was a problem. Type %s -h for help.\n", PACKAGE);
184       return 3;
185     }
186
187   FileList_t::iterator fi;
188   Result_t result = RESULT_OK;
189
190   for ( fi = Options.inFileList.begin(); ASDCP_SUCCESS(result) && fi != Options.inFileList.end(); fi++ )
191     {
192       if (Options.verbose_flag)
193         fprintf(stderr, "Opening file %s\n", ((*fi).c_str()));
194       
195       if ( Options.read_mxf_flag ) // dump MXF
196         {
197           Kumu::FileReader        Reader;
198           ASDCP::MXF::OPAtomHeader Header;
199           
200           result = Reader.OpenRead((*fi).c_str());
201           
202           if ( ASDCP_SUCCESS(result) )
203             result = Header.InitFromFile(Reader);
204           
205           Header.Dump(stdout);
206           
207           if ( ASDCP_SUCCESS(result) && Header.m_RIP.PairArray.size() > 3 )
208             {
209               MXF::Array<MXF::RIP::Pair>::const_iterator pi = Header.m_RIP.PairArray.begin();
210
211               for ( pi++; pi != Header.m_RIP.PairArray.end() && ASDCP_SUCCESS(result); pi++ )
212                 {
213                   result = Reader.Seek((*pi).ByteOffset);
214
215                   if ( ASDCP_SUCCESS(result) )
216                     {
217                       MXF::Partition TmpPart;
218                       result = TmpPart.InitFromFile(Reader);
219
220                       if ( ASDCP_SUCCESS(result) && TmpPart.BodySID > 0 )
221                         TmpPart.Dump(stdout);
222                     }
223                 }
224             }
225
226           if ( ASDCP_SUCCESS(result) )
227             {
228               ASDCP::MXF::OPAtomIndexFooter Index;
229               result = Reader.Seek(Header.FooterPartition);
230               
231               if ( ASDCP_SUCCESS(result) )
232                 {
233                   Index.m_Lookup = &Header.m_Primer;
234                   result = Index.InitFromFile(Reader);
235                 }
236               
237               if ( ASDCP_SUCCESS(result) )
238                 Index.Dump(stdout);
239             }
240         }
241       else // dump klv
242         {
243           Kumu::FileReader Reader;
244           KLVFilePacket KP;
245           
246           result = Reader.OpenRead((*fi).c_str());
247           
248           if ( ASDCP_SUCCESS(result) )
249             result = KP.InitFromFile(Reader);
250           
251           while ( ASDCP_SUCCESS(result) )
252             {
253               KP.Dump(stdout, true);
254               result = KP.InitFromFile(Reader);
255             }
256           
257           if( result == RESULT_ENDOFFILE )
258             result = RESULT_OK;
259         }
260     }
261
262   if ( ASDCP_FAILURE(result) )
263     {
264       fputs("Program stopped on error.\n", stderr);
265       
266       if ( result != RESULT_FAIL )
267         {
268           fputs(result, stderr);
269           fputc('\n', stderr);
270         }
271       
272       return 1;
273     }
274   
275   return 0;
276 }
277
278
279 //
280 // end klvwalk.cpp
281 //