fscking OpenBSD?
[asdcplib.git] / src / asdcp-test.cpp
index 6f4f2892fc94f8ef2df2132bd753569d3b2fd4e3..624c18bfad108cccc0cd9efc1b7cc0e96dcef9a4 100755 (executable)
@@ -1,5 +1,5 @@
 /*
-Copyright (c) 2003-2009, John Hurst
+Copyright (c) 2003-2011, John Hurst
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
@@ -103,7 +103,7 @@ banner(FILE* stream = stdout)
 {
   fprintf(stream, "\n\
 %s (asdcplib %s)\n\n\
-Copyright (c) 2003-2009 John Hurst\n\n\
+Copyright (c) 2003-2011 John Hurst\n\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",
@@ -115,10 +115,10 @@ void
 usage(FILE* stream = stdout)
 {
   fprintf(stream, "\
-USAGE: %s -c <output-file> [-3] [-b <buffer-size>] [-d <duration>] [-e|-E]\n\
-       [-f <start-frame>] [-j <key-id-string>] [-k <key-string>] [-l <label>]\n\
-       [-L] [-M] [-p <frame-rate>] [-R] [-s <num>] [-v] [-W]\n\
-       <input-file> [<input-file-2> ...]\n\
+USAGE: %s -c <output-file> [-3] [-a <uuid>] [-b <buffer-size>]\n\
+       [-d <duration>] [-e|-E] [-f <start-frame>] [-j <key-id-string>]\n\
+       [-k <key-string>] [-l <label>] [-L] [-M] [-p <frame-rate>] [-R]\n\
+       [-s <num>] [-v] [-W] [-z|-Z] <input-file> [<input-file-2> ...]\n\
 \n\
        %s [-h|-help] [-V]\n\
 \n\
@@ -166,16 +166,17 @@ Security Options:\n\
 
   fprintf(stream, "\
 Read/Write Options:\n\
+  -a <UUID>         - Specify the Asset ID of a file (with -c)\n\
   -b <buffer-size>  - Specify size in bytes of picture frame buffer.\n\
                       Defaults to 4,194,304 (4MB)\n\
   -d <duration>     - Number of frames to process, default all\n\
   -f <start-frame>  - Starting frame number, default 0\n\
   -l <label>        - Use given channel format label when writing MXF sound\n\
-                      files. SMPTE 429-2 labels: '5.1', '6.1', '7.1'. Default\n\
-                      is no label (valid for Interop only).\n\
+                      files. SMPTE 429-2 labels: '5.1', '6.1', '7.1', '7.1DS', 'WTF'.\n\
+                      Default is no label (valid for Interop only).\n\
   -L                - Write SMPTE UL values instead of MXF Interop\n\
   -p <rate>         - fps of picture when wrapping PCM or JP2K:\n\
-                      Use one of [23|24|48], 24 is default\n\
+                      Use one of [23|24|25|30|48|50|60], 24 is default\n\
   -R                - Repeat the first frame over the entire file (picture\n\
                       essence only, requires -c, -d)\n\
   -S                - Split Wave essence to stereo WAV files during extract.\n\
@@ -185,6 +186,8 @@ Read/Write Options:\n\
   -W                - Read input file only, do not write source file\n\
   -w <width>        - Width of numeric element in a series of frame file names\n\
                       (use with -x, default 6).\n\
+  -z                - Fail if j2c inputs have unequal parameters (default)\n\
+  -Z                - Ignore unequal parameters in j2c inputs\n\
 \n");
 
   fprintf(stream, "\
@@ -200,7 +203,7 @@ Other Options:\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\
          o An argument of \"23\" to the -p option will be interpreted\n\
-           as 23000/1001 fps.\n\
+           as 24000/1001 fps.\n\
 \n");
 }
 
@@ -231,8 +234,14 @@ decode_channel_fmt(const std::string& label_name)
   else if ( label_name == "7.1" )
     return PCM::CF_CFG_3;
 
+  else if ( label_name == "WTF" )
+    return PCM::CF_CFG_4;
+
+  else if ( label_name == "7.1DS" )
+    return PCM::CF_CFG_5;
+
   fprintf(stderr, "Error decoding channel format string: %s\n", label_name.c_str());
-  fprintf(stderr, "Expecting '5.1', '6.1', or '7.1'\n");
+  fprintf(stderr, "Expecting '5.1', '6.1', '7.1', '7.1DS' or 'WTF'\n");
   return PCM::CF_NONE;
 }
 
@@ -247,6 +256,7 @@ public:
   bool   error_flag;     // true if the given options are in error or not complete
   bool   key_flag;       // true if an encryption key was given
   bool   key_id_flag;    // true if a key ID was given
+  bool   asset_id_flag;  // true if an asset ID was given
   bool   encrypt_header_flag; // true if mpeg headers are to be encrypted
   bool   write_hmac;     // true if HMAC values are to be generated and written
   bool   read_hmac;      // true if HMAC values are to be validated
@@ -266,6 +276,7 @@ public:
   bool   duration_flag;  // true if duration argument given
   bool   do_repeat;      // if true and -c -d, repeat first input frame
   bool   use_smpte_labels; // if true, SMPTE UL values will be written instead of MXF Interop values
+  bool   j2c_pedantic;   // passed to JP2K::SequenceParser::OpenRead
   ui32_t picture_rate;   // fps of picture when wrapping PCM
   ui32_t fb_size;        // size of picture frame buffer
   ui32_t file_count;     // number of elements in filenames[]
@@ -273,6 +284,7 @@ public:
   const char* out_file;  // name of mxf file created by create mode
   byte_t key_value[KeyLen];  // value of given encryption key (when key_flag is true)
   byte_t key_id_value[UUIDlen];// value of given key ID (when key_id_flag is true)
+  byte_t asset_id_value[UUIDlen];// value of asset ID (when asset_id_flag is true)
   const char* filenames[MAX_IN_FILES]; // list of filenames to be processed
   PCM::ChannelFormat_t channel_fmt; // audio channel arrangement
 
@@ -280,7 +292,15 @@ public:
   Rational PictureRate()
   {
     if ( picture_rate == 23 ) return EditRate_23_98;
+    if ( picture_rate == 24 ) return EditRate_24;
+    if ( picture_rate == 25 ) return EditRate_25;
+    if ( picture_rate == 30 ) return EditRate_30;
     if ( picture_rate == 48 ) return EditRate_48;
+    if ( picture_rate == 50 ) return EditRate_50;
+    if ( picture_rate == 60 ) return EditRate_60;
+    if ( picture_rate == 96 ) return EditRate_96;
+    if ( picture_rate == 100 ) return EditRate_100;
+    if ( picture_rate == 120 ) return EditRate_120;
     return EditRate_24;
   }
 
@@ -288,18 +308,26 @@ public:
   const char* szPictureRate()
   {
     if ( picture_rate == 23 ) return "23.976";
+    if ( picture_rate == 24 ) return "24";
+    if ( picture_rate == 25 ) return "25";
+    if ( picture_rate == 30 ) return "30";
     if ( picture_rate == 48 ) return "48";
+    if ( picture_rate == 50 ) return "50";
+    if ( picture_rate == 60 ) return "60";
+    if ( picture_rate == 96 ) return "96";
+    if ( picture_rate == 100 ) return "100";
+    if ( picture_rate == 120 ) return "120";
     return "24";
   }
 
   //
   CommandOptions(int argc, const char** argv) :
-    mode(MMT_NONE), error_flag(true), key_flag(false), key_id_flag(false), encrypt_header_flag(true),
-    write_hmac(true), read_hmac(false), split_wav(false), mono_wav(false),
+    mode(MMT_NONE), error_flag(true), key_flag(false), key_id_flag(false), asset_id_flag(false),
+    encrypt_header_flag(true), write_hmac(true), read_hmac(false), split_wav(false), mono_wav(false),
     verbose_flag(false), fb_dump_size(0), showindex_flag(false), showheader_flag(false),
     no_write_flag(false), version_flag(false), help_flag(false), stereo_image_flag(false),
     number_width(6), start_frame(0),
-    duration(0xffffffff), duration_flag(false), do_repeat(false), use_smpte_labels(false),
+    duration(0xffffffff), duration_flag(false), do_repeat(false), use_smpte_labels(false), j2c_pedantic(true),
     picture_rate(24), fb_size(FRAME_BUFFER_SIZE), file_count(0), file_root(0), out_file(0),
     channel_fmt(PCM::CF_NONE)
   {
@@ -325,6 +353,21 @@ public:
              case '2': split_wav = true; break;
              case '3': stereo_image_flag = true; break;
 
+             case 'a':
+               asset_id_flag = true;
+               TEST_EXTRA_ARG(i, 'a');
+               {
+                 ui32_t length;
+                 Kumu::hex2bin(argv[i], asset_id_value, UUIDlen, &length);
+
+                 if ( length != UUIDlen )
+                   {
+                     fprintf(stderr, "Unexpected asset ID length: %u, expecting %u characters.\n", length, UUIDlen);
+                     return;
+                   }
+               }
+               break;
+
              case 'b':
                TEST_EXTRA_ARG(i, 'b');
                fb_size = abs(atoi(argv[i]));
@@ -429,6 +472,9 @@ public:
                file_root = argv[i];
                break;
 
+             case 'Z': j2c_pedantic = false; break;
+             case 'z': j2c_pedantic = true; break;
+
              default:
                fprintf(stderr, "Unrecognized option: %s\n", argv[i]);
                return;
@@ -516,7 +562,10 @@ write_MPEG2_file(CommandOptions& Options)
   if ( ASDCP_SUCCESS(result) && ! Options.no_write_flag )
     {
       WriterInfo Info = s_MyInfo;  // fill in your favorite identifiers here
-      Kumu::GenRandomUUID(Info.AssetUUID);
+      if ( Options.asset_id_flag )
+       memcpy(Info.AssetUUID, Options.asset_id_value, UUIDlen);
+      else
+       Kumu::GenRandomUUID(Info.AssetUUID);
 
       if ( Options.use_smpte_labels )
        {
@@ -751,10 +800,10 @@ write_JP2K_S_file(CommandOptions& Options)
     }
 
   // set up essence parser
-  Result_t result = ParserLeft.OpenRead(Options.filenames[0]);
+  Result_t result = ParserLeft.OpenRead(Options.filenames[0], Options.j2c_pedantic);
 
   if ( ASDCP_SUCCESS(result) )
-    result = ParserRight.OpenRead(Options.filenames[1]);
+    result = ParserRight.OpenRead(Options.filenames[1], Options.j2c_pedantic);
 
   // set up MXF writer
   if ( ASDCP_SUCCESS(result) )
@@ -773,7 +822,10 @@ write_JP2K_S_file(CommandOptions& Options)
   if ( ASDCP_SUCCESS(result) && ! Options.no_write_flag )
     {
       WriterInfo Info = s_MyInfo;  // fill in your favorite identifiers here
-      Kumu::GenRandomUUID(Info.AssetUUID);
+      if ( Options.asset_id_flag )
+       memcpy(Info.AssetUUID, Options.asset_id_value, UUIDlen);
+      else
+       Kumu::GenRandomUUID(Info.AssetUUID);
 
       if ( Options.use_smpte_labels )
        {
@@ -972,7 +1024,7 @@ write_JP2K_file(CommandOptions& Options)
   Kumu::FortunaRNG        RNG;
 
   // set up essence parser
-  Result_t result = Parser.OpenRead(Options.filenames[0]);
+  Result_t result = Parser.OpenRead(Options.filenames[0], Options.j2c_pedantic);
 
   // set up MXF writer
   if ( ASDCP_SUCCESS(result) )
@@ -992,7 +1044,10 @@ write_JP2K_file(CommandOptions& Options)
   if ( ASDCP_SUCCESS(result) && ! Options.no_write_flag )
     {
       WriterInfo Info = s_MyInfo;  // fill in your favorite identifiers here
-      Kumu::GenRandomUUID(Info.AssetUUID);
+      if ( Options.asset_id_flag )
+       memcpy(Info.AssetUUID, Options.asset_id_value, UUIDlen);
+      else
+       Kumu::GenRandomUUID(Info.AssetUUID);
 
       if ( Options.use_smpte_labels )
        {
@@ -1182,7 +1237,7 @@ write_PCM_file(CommandOptions& Options)
     {
       Parser.FillAudioDescriptor(ADesc);
 
-      ADesc.SampleRate = PictureRate;
+      ADesc.EditRate = PictureRate;
       FrameBuffer.Capacity(PCM::CalcFrameBufferSize(ADesc));
       ADesc.ChannelFormat = Options.channel_fmt;
 
@@ -1205,7 +1260,10 @@ write_PCM_file(CommandOptions& Options)
   if ( ASDCP_SUCCESS(result) && ! Options.no_write_flag )
     {
       WriterInfo Info = s_MyInfo;  // fill in your favorite identifiers here
-      Kumu::GenRandomUUID(Info.AssetUUID);
+      if ( Options.asset_id_flag )
+       memcpy(Info.AssetUUID, Options.asset_id_value, UUIDlen);
+      else
+       Kumu::GenRandomUUID(Info.AssetUUID);
 
       if ( Options.use_smpte_labels )
        {
@@ -1309,10 +1367,14 @@ read_PCM_file(CommandOptions& Options)
     {
       Reader.FillAudioDescriptor(ADesc);
 
-      if ( ADesc.SampleRate != EditRate_23_98
-          && ADesc.SampleRate != EditRate_24
-          && ADesc.SampleRate != EditRate_48 )
-       ADesc.SampleRate = Options.PictureRate();
+      if ( ADesc.EditRate != EditRate_23_98
+          && ADesc.EditRate != EditRate_24
+          && ADesc.EditRate != EditRate_25
+          && ADesc.EditRate != EditRate_30
+          && ADesc.EditRate != EditRate_48
+          && ADesc.EditRate != EditRate_50
+          && ADesc.EditRate != EditRate_60 )
+       ADesc.EditRate = Options.PictureRate();
 
       FrameBuffer.Capacity(PCM::CalcFrameBufferSize(ADesc));
 
@@ -1408,8 +1470,8 @@ write_timed_text_file(CommandOptions& Options)
   // set up MXF writer
   if ( ASDCP_SUCCESS(result) )
     {
-      Parser.FillDescriptor(TDesc);
-      FrameBuffer.Capacity(2*Kumu::Megabyte);
+      Parser.FillTimedTextDescriptor(TDesc);
+      FrameBuffer.Capacity(Options.fb_size);
 
       if ( Options.verbose_flag )
        {
@@ -1421,7 +1483,10 @@ write_timed_text_file(CommandOptions& Options)
   if ( ASDCP_SUCCESS(result) && ! Options.no_write_flag )
     {
       WriterInfo Info = s_MyInfo;  // fill in your favorite identifiers here
-      Kumu::GenRandomUUID(Info.AssetUUID);
+      if ( Options.asset_id_flag )
+       memcpy(Info.AssetUUID, Options.asset_id_value, UUIDlen);
+      else
+       Kumu::GenRandomUUID(Info.AssetUUID);
 
       if ( Options.use_smpte_labels )
        {
@@ -1519,8 +1584,8 @@ read_timed_text_file(CommandOptions& Options)
 
   if ( ASDCP_SUCCESS(result) )
     {
-      Reader.FillDescriptor(TDesc);
-      FrameBuffer.Capacity(2*Kumu::Megabyte);
+      Reader.FillTimedTextDescriptor(TDesc);
+      FrameBuffer.Capacity(Options.fb_size);
 
       if ( Options.verbose_flag )
        TimedText::DescriptorDump(TDesc);
@@ -1552,23 +1617,38 @@ read_timed_text_file(CommandOptions& Options)
     return result;
 
   std::string XMLDoc;
+  std::string out_path = Kumu::PathDirname(Options.file_root);
+  ui32_t write_count;
+  char buf[64];
   TimedText::ResourceList_t::const_iterator ri;
 
   result = Reader.ReadTimedTextResource(XMLDoc, Context, HMAC);
 
-  // do something with the XML here
-  fprintf(stderr, "XMLDoc size: %lu\n", XMLDoc.size());
+  if ( ASDCP_SUCCESS(result) )
+    {
+      Kumu::FileWriter Writer;
+      result = Writer.OpenWrite(Options.file_root);
+
+      if ( ASDCP_SUCCESS(result) )
+       result = Writer.Write(reinterpret_cast<const byte_t*>(XMLDoc.c_str()), XMLDoc.size(), &write_count);
+    }
 
   for ( ri = TDesc.ResourceList.begin() ; ri != TDesc.ResourceList.end() && ASDCP_SUCCESS(result); ri++ )
     {
-      result = Reader.ReadAncillaryResource((*ri).ResourceID, FrameBuffer, Context, HMAC);
+      result = Reader.ReadAncillaryResource(ri->ResourceID, FrameBuffer, Context, HMAC);
 
       if ( ASDCP_SUCCESS(result) )
        {
-         //      if ( Options.verbose_flag )
-           FrameBuffer.Dump(stderr, Options.fb_dump_size);
+         Kumu::FileWriter Writer;
+         result = Writer.OpenWrite(Kumu::PathJoin(out_path, Kumu::UUID(ri->ResourceID).EncodeHex(buf, 64)).c_str());
+
+         if ( ASDCP_SUCCESS(result) )
+           {
+             if ( Options.verbose_flag )
+               FrameBuffer.Dump(stderr, Options.fb_dump_size);
 
-         // do something with the resource data here
+             result = Writer.Write(FrameBuffer.RoData(), FrameBuffer.Size(), &write_count);
+           }
        }
     }
 
@@ -1635,7 +1715,7 @@ class MyTextDescriptor : public TimedText::TimedTextDescriptor
 {
  public:
   void FillDescriptor(TimedText::MXFReader& Reader) {
-    Reader.FillDescriptor(*this);
+    Reader.FillTimedTextDescriptor(*this);
   }
 
   void Dump(FILE* stream) {
@@ -1728,7 +1808,8 @@ show_file_info(CommandOptions& Options)
     {
       fprintf(stderr, "File is not AS-DCP: %s\n", Options.filenames[0]);
       Kumu::FileReader   Reader;
-      MXF::OPAtomHeader TestHeader;
+      const Dictionary* Dict = &DefaultCompositeDict();
+      MXF::OPAtomHeader TestHeader(Dict);
 
       result = Reader.OpenRead(Options.filenames[0]);
 
@@ -1737,15 +1818,15 @@ show_file_info(CommandOptions& Options)
 
       if ( ASDCP_SUCCESS(result) )
        {
-         TestHeader.Partition::Dump();
+         TestHeader.Partition::Dump(stdout);
 
          if ( MXF::Identification* ID = TestHeader.GetIdentification() )
-           ID->Dump();
+           ID->Dump(stdout);
          else
            fputs("File contains no Identification object.\n", stdout);
 
          if ( MXF::SourcePackage* SP = TestHeader.GetSourcePackage() )
-           SP->Dump();
+           SP->Dump(stdout);
          else
            fputs("File contains no SourcePackage object.\n", stdout);
        }
@@ -1827,6 +1908,12 @@ main(int argc, const char** argv)
   if ( Options.mode == MMT_INFO )
     {
       result = show_file_info(Options);
+
+      for ( int i = 1; ASDCP_SUCCESS(result) && i < Options.file_count; ++i )
+       {
+         Options.filenames[0] = Options.filenames[i]; // oh-so hackish
+         result = show_file_info(Options);
+       }
     }
   else if ( Options.mode == MMT_GOP_START )
     {
@@ -1853,15 +1940,10 @@ main(int argc, const char** argv)
     }
   else if ( Options.mode == MMT_UL_LIST )
     {
-      MDD_t di = (MDD_t)0;
-
-      while ( di < MDD_Max )
-       {
-         MDDEntry TmpType = Dict::Type(di);
-         UL TmpUL(TmpType.ul);
-         fprintf(stdout, "%s: %s\n", TmpUL.EncodeString(str_buf, 64), TmpType.name);
-         di = (MDD_t)(di + 1);
-       }
+      if ( Options.use_smpte_labels )
+       DefaultSMPTEDict().Dump(stdout);
+      else
+       DefaultInteropDict().Dump(stdout);
     }
   else if ( Options.mode == MMT_EXTRACT )
     {