2 Copyright (c) 2005-2013, John Hurst
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
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.
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.
36 using namespace ASDCP;
37 using Kumu::DefaultLogSink;
40 //------------------------------------------------------------------------------------------
42 // command line option parser class
44 static const char* PROGRAM_NAME = "klvsplit"; // program name for messages
45 typedef std::list<std::string> FileList_t;
47 // Increment the iterator, test for an additional non-option command line argument.
48 // Causes the caller to return if there are no remaining arguments or if the next
49 // argument begins with '-'.
50 #define TEST_EXTRA_ARG(i,c) if ( ++i >= argc || argv[(i)][0] == '-' ) \
52 fprintf(stderr, "Argument not found for option -%c.\n", (c)); \
58 banner(FILE* stream = stdout)
62 Copyright (c) 2005-2013 John Hurst\n\
63 %s is part of the asdcplib DCP tools package.\n\
64 asdcplib may be copied only under the terms of the license found at\n\
65 the top of every file in the asdcplib distribution kit.\n\n\
66 Specify the -h (help) option for further information about %s\n\n",
67 PROGRAM_NAME, ASDCP::Version(), PROGRAM_NAME, PROGRAM_NAME);
72 usage(FILE* stream = stdout)
75 USAGE: %s [-l <limit>] [-p <prefix>] [-s <suffix>] [-u] [-v] \n\
76 (<type-name>|<type-ul>) <mxf-filename>+\n\
82 -d - List the valid packet type names\n\
83 -h | -help - Show help\n\
84 -l <limit> - Stop processing after <limit> matching packets\n\
85 -p <prefix> - Use <prefix> to start output filenames (default\n\
86 uses the input filename minus any extension\n\
87 -s <suffix> - Append <suffix> to output filenames\n\
88 -u - Unwrap the packet value (i.e., do not output KL)\n\
89 -v - Verbose. Prints informative messages to stderr\n\
90 -V - Show version information\n\
92 NOTES: o There is no option grouping, all options must be distinct arguments.\n\
93 o All option arguments must be separated from the option by whitespace.\n\
94 \n", PROGRAM_NAME, PROGRAM_NAME, PROGRAM_NAME);
110 bool error_flag; // true if the given options are in error or not complete
111 bool version_flag; // true if the version display option was selected
112 bool help_flag; // true if the help display option was selected
113 bool verbose_flag; // true if the informative messages option was selected
114 bool unwrap_mode; // true if we are to strip the K and L before writing
116 ASDCP::UL target_ul; // a UL value identifying the packets to be extracted
117 ui64_t extract_limit; // limit extraction to the given number of packets
118 std::string prefix; // output filename prefix
119 std::string suffix; // output filename suffix
120 FileList_t inFileList; // File to operate on
122 CommandOptions(int argc, const char** argv, const ASDCP::Dictionary& dict) :
123 error_flag(true), version_flag(false), help_flag(false),
124 verbose_flag(false), unwrap_mode(false), mode(MMT_EXTRACT), extract_limit(ui64_C(-1))
126 for ( int i = 1; i < argc; ++i )
129 if ( (strcmp( argv[i], "-help") == 0) )
135 if ( argv[i][0] == '-' && isalpha(argv[i][1]) && argv[i][2] == 0 )
137 switch ( argv[i][1] )
139 case 'd': mode = MMT_LIST; break;
140 case 'h': help_flag = true; break;
143 TEST_EXTRA_ARG(i, 'l');
144 extract_limit = abs(strtoll(argv[i], 0, 10));
148 TEST_EXTRA_ARG(i, 'p');
153 TEST_EXTRA_ARG(i, 's');
157 case 'u': unwrap_mode = true; break;
158 case 'V': version_flag = true; break;
159 case 'v': verbose_flag = true; break;
162 fprintf(stderr, "Unrecognized option: %s\n", argv[i]);
166 else if ( argv[i][0] != '-' )
168 if ( ! target_ul.HasValue() )
170 if ( ! target_ul.DecodeHex(argv[i]) )
172 const ASDCP::MDDEntry *e = dict.FindSymbol(argv[i]);
178 if ( ! target_ul.HasValue() )
180 fprintf(stderr, "Value is not a UL or valid object name: %s\n", argv[i]);
186 inFileList.push_back(argv[i]);
191 fprintf(stderr, "Unrecognized option: %s\n", argv[i]);
196 if ( help_flag || version_flag )
199 if ( mode == MMT_EXTRACT )
201 if ( inFileList.empty() )
203 fputs("Input filename(s) required.\n", stderr);
207 if ( ! target_ul.HasValue() )
209 fputs("Packet UL not set. Use %s -u <ul> or keyword.\n", stderr);
219 //---------------------------------------------------------------------------------------------------
223 main(int argc, const char** argv)
225 const Dictionary *dict = &DefaultCompositeDict();
227 CommandOptions Options(argc, argv, *dict);
229 if ( Options.version_flag )
232 if ( Options.help_flag )
235 if ( Options.version_flag || Options.help_flag )
238 if ( Options.error_flag )
240 fprintf(stderr, "There was a problem. Type %s -h for help.\n", PROGRAM_NAME);
244 if ( Options.mode == MMT_LIST )
246 DefaultLogSink().UnsetFilterFlag(Kumu::LOG_ALLOW_WARN);
250 while ( di < MDD_Max )
252 const MDDEntry& e = dict->Type(di);
254 if ( e.name != 0 && ( e.ul[4] == 1 || e.ul[4] == 2 ) )
256 if ( Options.verbose_flag )
259 printf("%s %s\n", tmp_ul.EncodeString(buf, 64), e.name);
263 printf("%s\n", e.name);
267 di = (MDD_t)(di + 1);
273 Result_t result = RESULT_OK;
274 FileList_t::iterator fi;
276 for ( fi = Options.inFileList.begin(); ASDCP_SUCCESS(result) && fi != Options.inFileList.end(); ++fi )
278 if ( Options.verbose_flag )
279 fprintf(stderr, "Opening file %s\n", (fi->c_str()));
281 std::string this_prefix = Options.prefix.empty() ? Kumu::PathSetExtension(*fi, "") + "_" : Options.prefix;
282 Kumu::FileReader reader;
283 KLVFilePacket packet;
284 char filename_buf[1024], buf1[64], buf2[64];
285 ui64_t item_counter = 0;
287 result = reader.OpenRead(fi->c_str());
289 if ( KM_SUCCESS(result) )
290 result = packet.InitFromFile(reader);
292 while ( KM_SUCCESS(result) && item_counter < Options.extract_limit )
294 if ( packet.GetUL() == Options.target_ul
295 || packet.GetUL().MatchIgnoreStream(Options.target_ul) )
297 snprintf(filename_buf, 1024, "%s%010qu%s", this_prefix.c_str(), item_counter, Options.suffix.c_str());
299 if ( Options.verbose_flag )
300 fprintf(stderr, "%s (%d bytes)\n", filename_buf, packet.ValueLength());
302 Kumu::FileWriter writer;
303 writer.OpenWrite(filename_buf);
305 if ( KM_SUCCESS(result) )
307 if ( Options.unwrap_mode )
309 result = writer.Write(packet.m_Buffer.RoData() + packet.KLLength(), packet.ValueLength());
313 result = writer.Write(packet.m_Buffer.RoData(), packet.m_Buffer.Size());
320 if ( KM_SUCCESS(result) )
321 result = packet.InitFromFile(reader);
324 if ( result == RESULT_ENDOFFILE )
328 if ( ASDCP_FAILURE(result) )
330 fputs("Program stopped on error.\n", stderr);
332 if ( result != RESULT_FAIL )
334 fputs(result, stderr);