Added library names
[asdcplib.git] / src / klvwalk.cpp
index 3704f10f8e42b479fad033052af91d22125d8cbd..90b719b87544c4f791b7e18a60311a2111ac629b 100755 (executable)
@@ -1,10 +1,37 @@
-//
-// klvwalk.cpp
-//
-
-#include <AS_DCP.h>
-#include <MXF.h>
-#include <hex_utils.h>
+/*
+Copyright (c) 2005-2018, John Hurst
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+1. Redistributions of source code must retain the above copyright
+   notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+   notice, this list of conditions and the following disclaimer in the
+   documentation and/or other materials provided with the distribution.
+3. The name of the author may not be used to endorse or promote products
+   derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+/*! \file    klvwalk.cpp
+    \version $Id$
+    \brief   KLV+MXF test
+*/
+
+#include "AS_DCP.h"
+#include "MXF.h"
+#include <KM_log.h>
 #include <stdlib.h>
 #include <string.h>
 #include <ctype.h>
 #include <sys/stat.h>
 
 using namespace ASDCP;
+using Kumu::DefaultLogSink;
 
 
 //------------------------------------------------------------------------------------------
 //
+// command line option parser class
 
-// There is no header file thet defines this function.
-// You just have to know it's there...
-void set_debug_mode(bool info_mode, bool debug_mode);
+static const char* PROGRAM_NAME = "klvwalk";    // program name for messages
+typedef std::list<std::string> FileList_t;
 
+// Increment the iterator, test for an additional non-option command line argument.
+// Causes the caller to return if there are no remaining arguments or if the next
+// argument begins with '-'.
+#define TEST_EXTRA_ARG(i,c)    if ( ++i >= argc || argv[(i)][0] == '-' ) \
+                                 { \
+                                   fprintf(stderr, "Argument not found for option -%c.\n", (c)); \
+                                   return; \
+                                 }
 
-int
-main(int argc, char** argv)
+//
+void
+banner(FILE* stream = stdout)
 {
-  Result_t result = RESULT_OK;
-  bool read_mxf = false;
-  bool rewrite_mxf = false;
-  int arg_i = 1;
-  set_debug_mode(true, true);
+  fprintf(stream, "\n\
+%s (asdcplib %s)\n\n\
+Copyright (c) 2005-2013 John Hurst\n\
+%s is part of the asdcplib DCP tools package.\n\
+asdcplib may be copied only under the terms of the license found at\n\
+the top of every file in the asdcplib distribution kit.\n\n\
+Specify the -h (help) option for further information about %s\n\n",
+         PROGRAM_NAME, ASDCP::Version(), PROGRAM_NAME, PROGRAM_NAME);
+}
 
-  if ( strcmp(argv[1], "-r") == 0 )
-    {
-      read_mxf = true;
-      arg_i++;
-    }
-  else if ( strcmp(argv[1], "-w") == 0 )
-    {
-      rewrite_mxf = true;
-      arg_i++;
-      assert(argc - arg_i == 2);
-    }
+//
+void
+usage(FILE* stream = stdout)
+{
+  fprintf(stream, "\
+USAGE: %s [-r|-p] [-v] <input-file> [<input-file2> ...]\n\
+\n\
+       %s [-h|-help] [-V]\n\
+\n\
+  -h | -help   - Show help\n\
+  -r           - When KLV data is an MXF OPAtom or OP 1a file, display headers\n\
+  -p           - Display partition headers by walking the RIP\n\
+  -v           - Verbose. Prints informative messages to stderr\n\
+  -V           - Show version information\n\
+\n\
+  NOTES: o There is no option grouping, all options must be distinct arguments.\n\
+         o All option arguments must be separated from the option by whitespace.\n\
+\n", PROGRAM_NAME, PROGRAM_NAME);
+}
 
-  fprintf(stderr, "Opening file %s\n", argv[arg_i]);
+//
+//
+ class CommandOptions
+ {
+   CommandOptions();
+
+ public:
+   bool   error_flag;               // true if the given options are in error or not complete
+   bool   version_flag;             // true if the version display option was selected
+   bool   help_flag;                // true if the help display option was selected
+   bool   verbose_flag;             // true if the informative messages option was selected
+   bool   read_mxf_flag;            // true if the -r option was selected
+   bool   walk_parts_flag;          // true if the -p option was selected
+   FileList_t inFileList;           // File to operate on
+
+   CommandOptions(int argc, const char** argv) :
+     error_flag(true), version_flag(false), help_flag(false),
+     verbose_flag(false), read_mxf_flag(false), walk_parts_flag(false)
+   {
+     for ( int i = 1; i < argc; i++ )
+       {
+
+        if ( (strcmp( argv[i], "-help") == 0) )
+          {
+            help_flag = true;
+            continue;
+          }
+         
+        if ( argv[i][0] == '-' && isalpha(argv[i][1]) && argv[i][2] == 0 )
+          {
+            switch ( argv[i][1] )
+              {
+              case 'h': help_flag = true; break;
+              case 'r': read_mxf_flag = true; break;
+              case 'p': walk_parts_flag = true; break;
+              case 'V': version_flag = true; break;
+              case 'v': verbose_flag = true; break;
+
+              default:
+                fprintf(stderr, "Unrecognized option: %s\n", argv[i]);
+                return;
+              }
+          }
+        else
+          {
+            if ( argv[i][0] != '-' )
+              inFileList.push_back(argv[i]);
+
+            else
+              {
+                        fprintf(stderr, "Unrecognized option: %s\n", argv[i]);
+                return;
+              }
+          }
+       }
+
+     if ( help_flag || version_flag )
+       return;
+     
+     if ( inFileList.empty() )
+       {
+        fputs("Input filename(s) required.\n", stderr);
+        return;
+       }
+     
+     error_flag = false;
+   }
+ };
+
+
+//---------------------------------------------------------------------------------------------------
+//
 
-  if ( read_mxf )
-    {
-      ASDCP::FileReader        Reader;
-      ASDCP::MXF::OPAtomHeader Header;
+int
+main(int argc, const char** argv)
+{
+  CommandOptions Options(argc, argv);
 
-      result = Reader.OpenRead(argv[arg_i]);
+  if ( Options.version_flag )
+    banner();
 
-      if ( ASDCP_SUCCESS(result) )
-       result = Header.InitFromFile(Reader);
+  if ( Options.help_flag )
+    usage();
 
-      //      if ( ASDCP_SUCCESS(result) )
-      Header.Dump();
+  if ( Options.version_flag || Options.help_flag )
+    return 0;
 
-      if ( ASDCP_SUCCESS(result) )
-       {
-         ASDCP::MXF::OPAtomIndexFooter Index;
-         result = Reader.Seek(Header.FooterPartition);
+  if ( Options.error_flag )
+    {
+      fprintf(stderr, "There was a problem. Type %s -h for help.\n", PROGRAM_NAME);
+      return 3;
+    }
+
+  FileList_t::iterator fi;
+  Result_t result = RESULT_OK;
 
+  for ( fi = Options.inFileList.begin(); ASDCP_SUCCESS(result) && fi != Options.inFileList.end(); fi++ )
+    {
+      if (Options.verbose_flag)
+       fprintf(stderr, "Opening file %s\n", ((*fi).c_str()));
+      
+      if ( Options.read_mxf_flag ) // dump MXF
+       {
+         Kumu::FileReader        Reader;
+         const Dictionary* Dict = &DefaultCompositeDict();
+         ASDCP::MXF::OP1aHeader Header(Dict);
+         ASDCP::MXF::RIP RIP(Dict);
+         
+         result = Reader.OpenRead(*fi);
+         
          if ( ASDCP_SUCCESS(result) )
            {
-             Index.m_Lookup = &Header.m_Primer;
-             result = Index.InitFromFile(Reader);
+             result = MXF::SeekToRIP(Reader);
+             
+             if ( ASDCP_SUCCESS(result) )
+               {
+                 result = RIP.InitFromFile(Reader);
+                 ui32_t test_s = RIP.PairArray.size();
+
+                 if ( ASDCP_FAILURE(result) )
+                   {
+                     DefaultLogSink().Error("File contains no RIP\n");
+                     result = RESULT_OK;
+                   }
+                 else if ( RIP.PairArray.empty() )
+                   {
+                     DefaultLogSink().Error("RIP contains no Pairs.\n");
+                   }
+
+                 Reader.Seek(0);
+               }
+             else
+               {
+                 DefaultLogSink().Error("read_mxf SeekToRIP failed: %s\n", result.Label());
+               }
            }
 
          if ( ASDCP_SUCCESS(result) )
-           Index.Dump();
-       }
-    }
-  else if ( rewrite_mxf )
-    {
-      ASDCP::FileReader        Reader;
-      ASDCP::FileWriter        Writer;
-      ASDCP::MXF::OPAtomHeader Header;
-      ASDCP::MXF::OPAtomIndexFooter Index;
-
-      result = Reader.OpenRead(argv[arg_i++]);
-
-      if ( ASDCP_SUCCESS(result) )
-       result = Header.InitFromFile(Reader);
-
-      if ( ASDCP_SUCCESS(result) )
-       result = Reader.Seek(Header.FooterPartition);
-
-      if ( ASDCP_SUCCESS(result) )
-       result = Index.InitFromFile(Reader);
-
-      Header.m_Primer.ClearTagList();
-
-      if ( ASDCP_SUCCESS(result) )
-       result = Writer.OpenWrite(argv[arg_i]);
-
-      if ( ASDCP_SUCCESS(result) )
-       result = Header.WriteToFile(Writer);
+           result = Header.InitFromFile(Reader);
+         
+         if ( ASDCP_SUCCESS(result) )
+           Header.Dump(stdout);
+         
+         if ( ASDCP_SUCCESS(result) && RIP.PairArray.size() > 2 )
+           {
+             MXF::RIP::const_pair_iterator pi = RIP.PairArray.begin();
 
-//      if ( ASDCP_SUCCESS(result) )
-//     result = Index.WriteToFile(Writer);
+             for ( pi++; pi != RIP.PairArray.end() && ASDCP_SUCCESS(result); pi++ )
+               {
+                 result = Reader.Seek((*pi).ByteOffset);
 
-      // essence packets
+                 if ( ASDCP_SUCCESS(result) )
+                   {
+                     MXF::Partition TmpPart(Dict);
+                     result = TmpPart.InitFromFile(Reader);
 
-      // index
+                     if ( ASDCP_SUCCESS(result) && TmpPart.BodySID > 0 )
+                       TmpPart.Dump(stdout);
+                   }
+               }
+           }
 
-      // RIP
-    }
-  else // dump klv
-    {
-      ASDCP::FileReader Reader;
-      KLVFilePacket KP;
+         if ( ASDCP_SUCCESS(result) )
+           {
+             ASDCP::MXF::OPAtomIndexFooter Index(Dict);
+             result = Reader.Seek(Header.FooterPartition);
+             
+             if ( ASDCP_SUCCESS(result) )
+               {
+                 Index.m_Lookup = &Header.m_Primer;
+                 result = Index.InitFromFile(Reader);
+               }
+             
+             if ( ASDCP_SUCCESS(result) )
+               Index.Dump(stdout);
+           }
 
-      result = Reader.OpenRead(argv[arg_i]);
+         if ( ASDCP_SUCCESS(result) )
+           RIP.Dump(stdout);
+       }
+      else if ( Options.walk_parts_flag )
+       {
+         Kumu::FileReader        Reader;
+         const Dictionary* Dict = &DefaultCompositeDict();
+         ASDCP::MXF::OP1aHeader Header(Dict);
+         ASDCP::MXF::RIP RIP(Dict);
+         
+         result = Reader.OpenRead((*fi).c_str());
+         
+         if ( ASDCP_SUCCESS(result) )
+           result = MXF::SeekToRIP(Reader);
 
-      if ( ASDCP_SUCCESS(result) )
-       result = KP.InitFromFile(Reader);
+         if ( ASDCP_SUCCESS(result) )
+           {
+             result = RIP.InitFromFile(Reader);
+             ui32_t test_s = RIP.PairArray.size();
+
+             if ( ASDCP_FAILURE(result) )
+               {
+                 DefaultLogSink().Error("File contains no RIP\n");
+                 result = RESULT_OK;
+               }
+             else if ( RIP.PairArray.empty() )
+               {
+                 DefaultLogSink().Error("RIP contains no Pairs.\n");
+               }
+
+             Reader.Seek(0);
+           }
+         else
+           {
+             DefaultLogSink().Error("walk_parts SeekToRIP failed: %s\n", result.Label());
+           }
 
-      while ( ASDCP_SUCCESS(result) )
-       {
-         KP.Dump(stderr, true);
-         result = KP.InitFromFile(Reader);
+         if ( ASDCP_SUCCESS(result) )
+           {
+             RIP.Dump();
+
+             MXF::RIP::const_pair_iterator i;
+             for ( i = RIP.PairArray.begin(); i != RIP.PairArray.end(); ++i )
+               {
+                 Reader.Seek(i->ByteOffset);
+                 MXF::Partition plain_part(Dict);
+                 plain_part.InitFromFile(Reader);
+
+                 if ( plain_part.ThisPartition != i->ByteOffset )
+                   {
+                     DefaultLogSink().Error("ThisPartition value error: wanted=%qu, got=%qu\n",
+                                            plain_part.ThisPartition, i->ByteOffset);
+                   }
+
+                 plain_part.Dump();
+               }
+           }
        }
+      else // dump klv
+       {
+         Kumu::FileReader Reader;
+         KLVFilePacket KP;
+         ui64_t pos = 0;
 
-      if( result == RESULT_ENDOFFILE )
-       result = RESULT_OK;
+         result = Reader.OpenRead((*fi).c_str());
+         
+         if ( ASDCP_SUCCESS(result) )
+           result = KP.InitFromFile(Reader);
+         
+         while ( ASDCP_SUCCESS(result) )
+           {
+             fprintf(stdout, "@0x%08llx: ", pos);
+             KP.Dump(stdout, DefaultCompositeDict(), true);
+             pos = Reader.Tell();
+             result = KP.InitFromFile(Reader);
+           }
+         
+         if( result == RESULT_ENDOFFILE )
+           result = RESULT_OK;
+       }
     }
 
-  if ( result != RESULT_OK )
+  if ( ASDCP_FAILURE(result) )
     {
       fputs("Program stopped on error.\n", stderr);
-
+      
       if ( result != RESULT_FAIL )
        {
-         fputs(GetResultString(result), stderr);
+         fputs(result, stderr);
          fputc('\n', stderr);
        }
-
+      
       return 1;
     }
-
+  
   return 0;
 }
 
@@ -149,13 +372,3 @@ main(int argc, char** argv)
 //
 // end klvwalk.cpp
 //
-
-
-
-
-
-
-
-
-
-