ISXDDataEssenceDescriptor_NamespaceURI UL fixed
[asdcplib.git] / src / DCData_ByteStream_Parser.cpp
1 /*
2 Copyright (c) 2013-2013, 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    DCData_ByteStream_Parser.cpp
28     \version $Id$
29     \brief   AS-DCP library, Digital Cinema Data bytestream essence reader
30 */
31
32 #include "AS_DCP.h"
33 #include "KM_fileio.h"
34 #include "KM_log.h"
35 using Kumu::DefaultLogSink;
36
37 //------------------------------------------------------------------------------------------
38
39 class ASDCP::DCData::BytestreamParser::h__BytestreamParser
40 {
41   ASDCP_NO_COPY_CONSTRUCT(h__BytestreamParser);
42
43 public:
44   DCDataDescriptor  m_DDesc;
45   Kumu::FileReader   m_File;
46
47   h__BytestreamParser()
48   {
49     memset(&m_DDesc, 0, sizeof(m_DDesc));
50     m_DDesc.EditRate = Rational(24,1);
51   }
52
53   ~h__BytestreamParser() {}
54
55   Result_t OpenReadFrame(const std::string& filename, FrameBuffer& FB)
56   {
57     m_File.Close();
58     Result_t result = m_File.OpenRead(filename);
59
60     if ( ASDCP_SUCCESS(result) )
61     {
62         Kumu::fsize_t file_size = m_File.Size();
63
64         if ( FB.Capacity() < file_size )
65         {
66             DefaultLogSink().Error("FrameBuf.Capacity: %u frame length: %u\n", FB.Capacity(), (ui32_t)file_size);
67             return RESULT_SMALLBUF;
68         }
69     }
70
71     ui32_t read_count;
72
73     if ( ASDCP_SUCCESS(result) )
74         result = m_File.Read(FB.Data(), FB.Capacity(), &read_count);
75
76     if ( ASDCP_SUCCESS(result) )
77         FB.Size(read_count);
78
79     return result;
80   }
81 };
82
83
84 //------------------------------------------------------------------------------------------
85
86 ASDCP::DCData::BytestreamParser::BytestreamParser()
87 {
88 }
89
90 ASDCP::DCData::BytestreamParser::~BytestreamParser()
91 {
92 }
93
94 // Opens the stream for reading, parses enough data to provide a complete
95 // set of stream metadata for the MXFWriter below.
96 ASDCP::Result_t
97 ASDCP::DCData::BytestreamParser::OpenReadFrame(const std::string& filename, FrameBuffer& FB) const
98 {
99   const_cast<ASDCP::DCData::BytestreamParser*>(this)->m_Parser = new h__BytestreamParser;
100   return m_Parser->OpenReadFrame(filename, FB);
101 }
102
103 //
104 ASDCP::Result_t
105 ASDCP::DCData::BytestreamParser::FillDCDataDescriptor(DCDataDescriptor& DDesc) const
106 {
107   if ( m_Parser.empty() )
108     return RESULT_INIT;
109
110   DDesc = m_Parser->m_DDesc;
111   return RESULT_OK;
112 }
113
114
115 //
116 // end DCData_Bytestream_Parser.cpp
117 //
118
119
120