ginormo merge-back with Kumu, SMPTE MIC key and MPEG parser fix
[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 const char* PACKAGE = "klvwalk";
46
47
48 //------------------------------------------------------------------------------------------
49 //
50
51
52 int
53 main(int argc, char** argv)
54 {
55   Result_t result = RESULT_OK;
56   bool read_mxf = false;
57   int arg_i = 1;
58
59   if ( argc > arg_i && strcmp(argv[1], "-r") == 0 )
60     {
61       read_mxf = true;
62       arg_i++;
63     }
64
65   if ( argc - arg_i != 1 )
66     {
67       fprintf(stderr, "usage: %s [-r] <infile>\n", PACKAGE);
68       return 1;
69     }
70
71   fprintf(stderr, "Opening file %s\n", argv[arg_i]);
72
73   if ( read_mxf )
74     {
75       Kumu::FileReader        Reader;
76       ASDCP::MXF::OPAtomHeader Header;
77
78       result = Reader.OpenRead(argv[arg_i]);
79
80       if ( ASDCP_SUCCESS(result) )
81         result = Header.InitFromFile(Reader);
82
83       Header.Dump(stdout);
84
85       if ( ASDCP_SUCCESS(result) )
86         {
87           ASDCP::MXF::OPAtomIndexFooter Index;
88           result = Reader.Seek(Header.FooterPartition);
89
90           if ( ASDCP_SUCCESS(result) )
91             {
92               Index.m_Lookup = &Header.m_Primer;
93               result = Index.InitFromFile(Reader);
94             }
95
96           if ( ASDCP_SUCCESS(result) )
97             Index.Dump(stdout);
98         }
99     }
100   else // dump klv
101     {
102       Kumu::FileReader Reader;
103       KLVFilePacket KP;
104
105       result = Reader.OpenRead(argv[arg_i]);
106
107       if ( ASDCP_SUCCESS(result) )
108         result = KP.InitFromFile(Reader);
109
110       while ( ASDCP_SUCCESS(result) )
111         {
112           KP.Dump(stdout, true);
113           result = KP.InitFromFile(Reader);
114         }
115
116       if( result == RESULT_ENDOFFILE )
117         result = RESULT_OK;
118     }
119
120   if ( ASDCP_FAILURE(result) )
121     {
122       fputs("Program stopped on error.\n", stderr);
123
124       if ( result != RESULT_FAIL )
125         {
126           fputs(result, stderr);
127           fputc('\n', stderr);
128         }
129
130       return 1;
131     }
132
133   return 0;
134 }
135
136
137 //
138 // end klvwalk.cpp
139 //